Is there a pop functionality for solidity arrays?

前端 未结 3 1372
失恋的感觉
失恋的感觉 2021-02-20 10:36

I have used solidity to push data into an array. Is there a similar function for pop ?

string[] myArray;
myArray.push(\"hello\")

What is the be

3条回答
  •  轮回少年
    2021-02-20 11:28

    You can try...

    pragma solidity ^0.4.17;
    
    contract TestArray {
       uint[] public items;
    
       constructor () public {
          items.push(1);
          items.push(2);
          items.push(3);
          items.push(4);
       }
    
       function pushElement(uint value) public {
          items.push(value);
       }
    
       function popElement() public returns (uint []){
          delete items[items.length-1];
          items.length--;
          return items;
       }
    
       function getArrayLength() public view returns (uint) {
          return items.length;
       }
    
       function getFirstElement() public view returns (uint) {
          return items[0];
       }
    
       function getAllElement()  public view returns (uint[]) {
          return items;
       }
    }
    

提交回复
热议问题