Search code examples
ethereumsolidity

Dynamic array in Solidity


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

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


Solution

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