Index
length in array
What nodes.length=0; meanspragma solidity >=0.4.22 <0.7.0;
contract TestNode {
uint[] nodes;
function createNode(uint data) public {
nodes.length = 0;
nodes.push(data);
}
function pushNode(uint data) public {
nodes.push(data);
}
function getNode() public view returns(uint[] memory) {
return nodes;
}
}
contract TestNode {
uint[] nodes;
function createNode(uint data) public {
nodes.length = 0;
nodes.push(data);
}
function pushNode(uint data) public {
nodes.push(data);
}
function getNode() public view returns(uint[] memory) {
return nodes;
}
}
getNode
output:
0: uint256[]:
Empty value
pushNode(5)
getNode
output:
0: uint256[]: 5
pushNode(10)
output:
0: uint256[]: 5,10
createNode(99)
getNode
output:
0: uint256[]: 99
See the createNode(99) sets the length of the array to zero
and then push 99 to create a array with length 1