数组
数组可以在声明时指定长度,也可以动态调整大小。 对于 存储storage 的数组来说,元素类型可以是任意的(即元素也可以是数组类型,映射类型或者结构体)。 对于 内存memory 的数组来说,元素类型不能是映射类型,如果作为 public 函数的参数,它只能是 ABI 类型。
一个元素类型为 T,固定长度为 k 的数组可以声明为 T[k],而动态数组声明为 T[]。 举个例子,一个长度为 5,元素类型为 uint 的动态数组的数组,应声明为 uint[][5] (注意这里跟其它语言比,数组长度的声明位置是反的)。 要访问第三个动态数组的第二个元素,你应该使用 x[2][1](数组下标是从 0 开始的,且访问数组时的下标顺序与声明时相反,也就是说,x[2] 是从右边减少了一级)。
contract array {
uint256[] public arr; // 不定长数组
uint256[10] public fixedArr; // 定长数组
uint256[] public arr2 = [1,32,3,221,12,12,12123123];
// 添加值
function pushArr(uint256 _a) public {
arr.push(_a);
}
// 删除值
function popArr() public {
arr.pop();
}
// 删除指定位置值
function del(uint256 _index) public {
delete arr[_index];
}
}
Mapping
映射类型在声明时的形式为 mapping(_KeyType => _ValueType)。 其中 _KeyType 可以是除了映射、变长数组、合约、枚举以及结构体以外的几乎所有类型。 _ValueType 可以是包括映射类型在内的任何类型。
映射可以视作 哈希表 https://en.wikipedia.org/wiki/Hash_table,它们在实际的初始化过程中创建每个可能的 key, 并将其映射到字节形式全是零的值:一个类型的 默认值。然而下面是映射与哈希表不同的地方: 在映射中,实际上并不存储 key,而是存储它的 keccak256 哈希值,从而便于查询实际的值。
正因为如此,映射是没有长度的,也没有 key 的集合或 value 的集合的概念。
只有状态变量(或者在 internal 函数中的对于存储变量的引用)可以使用映射类型。。
可以将映射声明为 public,然后来让 Solidity 创建一个 getter。 _KeyType 将成为 getter 的必须参数,并且 getter 会返回 _ValueType。
_ValueType 也可以是一个映射。这时在使用 getter 时将将需要递归地传入每个 _KeyType 参数。
contract Test {
mapping (address => uint256) public balanceOf;
function getbalance(address _address) public view returns (uint256) {
return balanceOf[_address];
}
function setBalance(address _address, uint256 _amount) public {
require(_amount>0,'amount miss > 0');
balanceOf[_address] += _amount;
}
function remove(address addr) public {
delete balanceOf[addr];
}
}
Enum
contract test {
enum ActionChoices { GoLeft, GoRight, GoStraight, SitStill }
ActionChoices choice;
ActionChoices constant defaultChoice = ActionChoices.GoStraight;
function setGoStraight() public {
choice = ActionChoices.GoStraight;
}
// 由于枚举类型不属于 |ABI| 的一部分,因此对于所有来自 Solidity 外部的调用,
// "getChoice" 的签名会自动被改成 "getChoice() returns (uint8)"。
// 整数类型的大小已经足够存储所有枚举类型的值,随着值的个数增加,
// 可以逐渐使用 `uint16` 或更大的整数类型。
function getChoice() public view returns (ActionChoices) {
return choice;
}
function getDefaultChoice() public pure returns (uint) {
return uint(defaultChoice);
}
}
结构体
pragma solidity ^0.8.0;
contract CrowdFunding {
struct Funder {
address addr;
uint256 amount;
}
uint num;
mapping (uint256 => Funder) public funder;
function set(address addr, uint256 amount) public {
funder[num] = Funder(addr,amount);
num++;
}
}