A "2D" array is treated as an array of arrays. For example, if you needed a 10 x 2, it would be a uint[10]
in which each row is itself a uint[2]
. Either dimension can be dynamic.
A common mistake is to reverse the order of the indexes. For example, a dynamic list of uint[3]uint[3]
would be:
uint[3][] myArray;
Later, when you want to set, say the 2nd element of row 100, it would be:
myArray[100][2]
because there is a uint[3]uint[3]
stored in row 100 and we want the 2nd element in that array.
This may help you find your way around dynamic x dynamic. Remember, weird switcharoos happen if it seems like it's not working.
pragma solidity 0.5.0;
contract TwoD {
uint[][] public test;
function appendFirstOrderArray() public {
uint[] memory element;
test.push(element);
}
function appendSecondOrderElementAtRow(uint row, uint value) public {
test[row].push(value);
}
function getFirstOrderLength() public view returns(uint) {
return test.length;
}
function getSecondOrderLengthAtRow(uint row) public view returns(uint) {
return test[row].length;
}
}
Hope it helps.