Dynamic array in Solidity

▼魔方 西西 提交于 2019-12-06 07:48:44

问题


I a very new to Ethereum and Solidity development.

I just want to declare a simple array ( dynamic list ), one set function to push string in that and one get a function which returns all the strings saved in the dynamic array.

I search a lot but not able to find this simple stuff.

Thanks in advance


回答1:


Here is my solution, you need experimental ABIEncoderV2 to return array of strings.

pragma solidity ^0.5.2;
pragma experimental ABIEncoderV2;

contract Test {

    string[] array;

    function push(string calldata _text) external {
        array.push(_text);
    }

    function get() external view returns(string[] memory) {
        return array;
    }
}



回答2:


If, finally, you want to interact with your smart contract with tools like web3j (for java) or web3js (javascript) in an application, working with dynamic arrays is not going to work because of some bugs in those libraries.
In this case you should serialize your output array. Same applies if you have an input array.



来源:https://stackoverflow.com/questions/53985923/dynamic-array-in-solidity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!