Index

Solidity

  1. types
  2. External
  3. Library
    1. self in Library
    2. using * for
    3. self example
  4. interfaces
  5. import
  6. msg.value
  7. Handling ether
  8. Erc20 token in contract
  9. assembly
  10. Enum
  11. memory, storage, calldata
  12. Array Pushing
  13. length in array
  14. Struct array
  15. mapping
  16. bytes and string
  17. uint to bytes32
  18. returns with name
  19. score voting
  20. Tests
    1. Async Tests
    2. Hooks

length in array

What nodes.length=0; means

pragma 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;
    }
}


images/830-1.png

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