Skip to content

Instantly share code, notes, and snippets.

@gwmccubbin
Created March 30, 2020 16:41
Show Gist options
  • Save gwmccubbin/e0e26032466d95ba5dd2273d88889852 to your computer and use it in GitHub Desktop.
Save gwmccubbin/e0e26032466d95ba5dd2273d88889852 to your computer and use it in GitHub Desktop.
pragma solidity ^0.6.0;
contract MyContract {
// Arrays
uint[] public uintArray = [1,2,3];
string[] public stringArray = ['apple', 'banana', 'carrot'];
string[] public values;
uint[][] public array2D = [ [1,2,3], [4,5,6] ];
function addValue(string memory _value) public {
values.push(_value);
}
function valueCount() public view returns(uint) {
return values.length;
}
}
@skyler620
Copy link

Hi, I did as exactly to your coding. But when I try to add value to the array2D. It shows error. What did I do wrong on this part? Appreciate for your answer

@callezenwaka
Copy link

callezenwaka commented May 4, 2022

The code below works fine.

 function addArray2D(uint[] memory _array) public {
      // append to array2D
      array2D.push(_array);
  }

function getArray2D(uint index1, uint index2) public view returns(uint) {
    // get element at specific index
    return array2D[index1][index2];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment