I am currently playing around with Solidity and dynamic arrays. I have the following code:
contract Test{
int256[] public test;
function setTest(int256 b) public{
test.push(b);
}
function setTest2(uint256 a, int256 b) public {
test[a] = b;
}
}
I executed the code in remix. When I call setTest(1) and call the getter test(0), the output is 1. If I do the same with setTest2(0, 1) and call the getter afterwards, the value does not seem to be saved. I don't understand why. Could someone explain what is going on here?
Thanks in advance.
I realized that my question is not really clear and I will ask the question here and this time more clear. Thanks for the other answers already.
I call the function setTest2(0, 1)
and call the getter afterwards, remix shows me that the value at position 0 is 0 and not 1.
When I call the setTest(2)
function and call the getter afterwards, the value 2 is at position 0 of the array and not at position 1. This means that the value of the first call has not been saved. Why?
setTest2(0,1)
on an array of a length of 0, it won't work.