Q:

given address[] wallets. What is the correct method to check that the list contains a given address?

Does solidity provide any native list contains function?

If not, is the only way to do a full iteration of the array and check each element?

Are there more memory efficient ways of doing this, maybe a mapping?

A:

Solidity doesn't provide a contains method, you'd have to manually iterate and check.

Using an array for what you're trying to achieve would be a highly inefficient pattern. The best and most cost efficient method is use a mapping data structure. Set the key to be the address and the value to be a boolean. Lists that are too long have the possibility of running out of gas when you're trying to iterate over them.

If you need to iterate through all the keys in the mapping, then you'd need to have an external database to get all the keys. The database can be populated and updated based on events from the smart contract (i.e. an event when the address is added or removed).

最高效的方式不是 循环遍历数组(可能耗光所有的gas),而是 使用mapping,去做。

Example:

contract myWallets
{
mapping (address => bool) public Wallets; function setWallet(address _wallet) public{
Wallets[_wallet]=true;
} function contains(address _wallet) returns (bool){
return Wallets[_wallet];
}
}

Solidity的地址 数组如何判断是否包含一个给定的地址?的更多相关文章

  1. 项目SVN的IP地址发生变化时修改SVN为新的IP地址

    在eclipse或者Myeclipse自带的svn:subclipse中修改ip地址 项目开发中有可能要修改SVN的IP地址,entries文件里面包含svn服务器的地址信息.每个文件夹都会产生一个e ...

  2. C#反射实现 C# 反射 判断类的延伸类型 使用代码生成工具Database2Sharp快速生成工作流模块控制器和视图代码 C# ADO.NET的SqlDataReader对象,判断是否包含指定字段 页面中添加锚点的几种方式 .net 简单实用Log4net(多个日志配置文件) C# 常用小点

    C#反射实现   一.反射概念: 1.概念: 反射,通俗的讲就是我们在只知道一个对象的内部而不了解内部结构的情况下,通过反射这个技术可以使我们明确这个对象的内部实现. 在.NET中,反射是重要的机制, ...

  3. PHP 判断是否包含某字符串

    PHP语言是一个功能强大的嵌入式HTML脚本语言,它的易用性让许多程序员选择使用.PHP判断字符串的包含,可以使用PHP的内置函数 strstr,strpos,stristr直接进行判断.也可以通过e ...

  4. PHP 判断是否包含在某个字符串中

    1.用strpos函数,查找字符首次出现的位置,如果不存在则会返回false$str= 'abc';$needle= 'e';$pos = strpos($str, $needle);2.用strst ...

  5. JS判断一个字符串是否包含一个子串函数.

    微信小程序 JS判断一个字符串是否包含一个子串函数. //str 字符串,name子串     contains:function(str,name){         if(str.indexOf( ...

  6. 【转载】C#中List集合使用Contains方法判断是否包含某个对象

    在C#的List集合中,如果要查找List集合是否包含某一个值或者对象,如果不使用List集合类的扩展方法的话一般会使用for循环或者foreach遍历来查找,其实List集合类中的扩展方法Conta ...

  7. php 判断字符串包含

    PHP语言是一个功能强大的嵌入式HTML脚本语言,它的易用性让许多程序员选择使用.PHP判断字符串的包含,可以使用PHP的内置函数 strstr,strpos,stristr直接进行判断.也可以通过e ...

  8. Javascript中数组的判断方法

    摘要: 1.数组检测的方法: 1) typeof . 2) instanceof . 3) constructor . 4) Object.prototype.toString. 5) Array.i ...

  9. struts2 用if标签判断字符串包含

    String testStr = "用来判断是否包含的字符串"; <s:property value="testStr"/> <s:if te ...

随机推荐

  1. ResourceBundle类的方式来读取config.properties配置文件参数值

    //获取config.properties配置文件参数值 public static ResourceBundle resource = ResourceBundle.getBundle(" ...

  2. dubbo 熔断,限流,降级

    1 写在前面 1.1 名词解释 consumer表示服务调用方 provider标示服务提供方,dubbo里面一般就这么讲. 下面的A调用B服务,一般是泛指调用B服务里面的一个接口. 1.2 拓扑图 ...

  3. mySQL 判断表是否存

    select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA`='数据库名' and `TABLE_NAME`= ...

  4. mongo数据的导入(mongoimport)和导出(mongoexport)

    1.导出数据可以使用命令: mongoexport -h dbhost -d dbname -c collectionName -o output 参数说明: -h 数据库地址 -d 指明使用的库 - ...

  5. 前端-JavaScript1-4——JavaScript之变量

    变量(Variables),和高中代数学习的x.y.z很像,它们不是字母,而是蕴含值的符号. 它和直接量不同,直接量5,就是数字5:直接量”你好”就是字符串“你好”.现在这个变量不一样了,你看见一个a ...

  6. 基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络

    基础 在参考①中我们详细介绍了没有隐含层的神经网络结构,该神经网络只有输入层和输出层,并且输入层和输出层是通过全连接方式进行连接的.具体结构如下: 我们用此网络结构基于MNIST数据集(参考②)进行训 ...

  7. cookie.js插件

    /*! cookiejs v1.0.23 | MIT (c) 2018 kenny wong | https://github.com/jaywcjlove/cookie.js */!function ...

  8. Pycharm2018的激活方法或破解方法(必须加host)

    修改hosts文件将0.0.0.0 account.jetbrains.com添加到hosts文件最后,注意hosts文件无后缀,如果遇到无法修改或权限问题,可以采用覆盖的方法去替换hosts文件 修 ...

  9. Ajax提交from表单

    一,使用Ajax提交form表单到后台传参问题 1,首先,定义一个form: <form class="form-horizontal" role="form&qu ...

  10. 5、Zookeeper命令操作

    一.Zookeeper的四字命令 Zookeeper支持某些特定的四字命令字母与其的交互.他们大多数是查询命令,用来获取Zookeeper服务的当前状态及相关信息.用户在客户端可以通过telnet或n ...