Solidity中的动态数组 [英] Dynamic array in Solidity

查看:519
本文介绍了Solidity中的动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以太坊和Solidity开发非常陌生。

I a very new to Ethereum and Solidity development.

我只想声明一个简单的数组(dynamic list),并在其中声明一个用于推送字符串的函数一个获取返回动态数组中保存的所有字符串的函数。

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.

预先感谢

推荐答案

这是我的解决方案,您需要 experimental

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

这篇关于Solidity中的动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆