数组(Arrays)
数组可以声明时指定大小,也可以是动态变长。对于storage存储的数组来说,元素类型可以是任意的(可以是数组,map,结构体)。对于memory存储的数组,如果作为public函数的参数,它不能是map类型的数组,只能是支持ABI的类型。
一个数组,固定大小为k,元素类型为T,可以声明为T[k],如果数组大小是动态(变长)的,可以声明为T[]。如声明一个类型为uint的数组长度为5的变长数组(5个元素都是变长数组),可以声明为uint[][5]。(注意,相比其他语言,solidity多维数组的长度声明是反的。)要访问第三个动态数组的第二个元素,使用x[2][1](数组的索引是从0开始的,访问数组元素的顺序与声明时相反)。
bytes和string是特殊的数组。bytes和byte[]是类似的,但在外部函数作为参数调用中,bytes会进行压缩打包。string和bytes类似,但是不允许长度和索引的方式访问(目前)。
因此,优先使用bytes而不是byte[],因为bytes更节省内存。
注意:
如果你想访问字符串的某一个字节,你可以通过bytes(s).length / bytes(s)[7] = 'x';的方式来访问。记住,你访问的是UTF-8表示的低级别的字节,而不是独立的字符。
这句话有点绕,我尝试用自己的理解解释一下。可以将字符串s通过bytes(s)转为一个bytes,可以通过bytes(s).length获取长度,bytes(s)[n]获取对应的UTF-8编码。通过下标访问获取到的不是对应字符,而是UTF-8编码,比如中文编码是多字节,变长的,所以下标访问到的只是其中的一个编码。
类型为数组的状态变量,可以标记为public,从而让Solidity创建一个访问器getter,可以通过索引的方式来访问数组的某个元素。
创建内存数组(Allocating Memory Arrays)
可使用new关键字创建一个memory的数组。与stroage数组不同的是,你不能通过.length来修改数组大小。
pragma solidity ^0.4.16;
contract C {
function f(uint len) public pure {
uint[] memory a = new uint[](7);
bytes memory b = new bytes(len);
// Here we have a.length == 7 and b.length == len
a[6] = 8;
//错误,编译不通过
// a.length = 10;
}
}
数组常量/内联数组(Array Literals / Inline Arrays)
数组常量,是一个还没有赋值到变量的数组表达式。
pragma solidity ^0.4.16;
contract C {
function f() public pure {
//编译不通过
//g([1, 2, 3]);
g([uint(1), 2, 3]);
}
function g(uint[3] _data) public pure {
// ...
}
}
数组常量是一个固定大小的memory数组,元素类型则是使用刚好能存储的元素的能用类型,比如[1, 2, 3],只需要uint8即可存储,因为这几个元素的类型都是uint8。它的类型是uint8[3] memory。
由于g()方法的参数需要的是uint(uint表示的是uint256),所以需要对第一个元素进行类型转换,使用uint来进行这个转换。
注意:定长的memory数组是不能和变长的memory数组之间相互赋值的。
// 不能编译通过
pragma solidity ^0.4.0;
contract C {
function f() public {
// The next line creates a type error because uint[3] memory
// cannot be converted to uint[] memory.
uint[] memory x = [uint(1), 3, 4];
}
}
已经计划在未来移除这样的限制。当前因为ABI传递数组还有些问题。
成员(Members)
length:
数组有一个.length属性,表示当前的数组长度。storage的变长数组(memory不可以),可以通过给.length赋值调整数组长度。memory的变长数组不支持。不能通过访问超出当前数组的长度的方式,来自动实现改变数组长度。memory数组虽然可以通过参数,灵活指定大小,但一旦创建,大小不可调整。
push:
变长的storage数组和bytes都有一个成员方法push()(string没有),用于在数组的末尾添加一个新元素,返回值为新的长度。
pop:
变长的storage数组和bytes都有一个成员方法push()(string没有),用于移除数组末尾的元素。
警告:
在external(外部)函数中,不能使用多维数组
基于EVM的限制,不能通过external(外部)函数返回动态的内容。
在下面这个的例子中,如果通过web3.js调用能返回数据,但从Solidity中调用不能返回数据。
目前唯一的解决办法是使用一个非常大的静态数组。
contract C {
function f() returns (uint[]) { ... }
}
pragma solidity ^0.4.16;
contract ArrayContract {
uint[2**20] m_aLotOfIntegers;
// 这里不是两个动态数组的数组,而是一个动态数组里,每个元素是长度为二的数组。
bool[2][] m_pairsOfFlags;
// newPairs 存在 memory里,因为是函数参数
function setAllFlagPairs(bool[2][] newPairs) public {
m_pairsOfFlags = newPairs;
}
function setFlagPair(uint index, bool flagA, bool flagB) public {
// 访问不存在的index会抛出异常
m_pairsOfFlags[index][0] = flagA;
m_pairsOfFlags[index][1] = flagB;
}
function changeFlagArraySize(uint newSize) public {
// 如果新size更小, 移除的元素会被销毁
m_pairsOfFlags.length = newSize;
}
function clear() public {
// 销毁
delete m_pairsOfFlags;
delete m_aLotOfIntegers;
// 同销毁一样的效果
m_pairsOfFlags.length = 0;
}
bytes m_byteData;
function byteArrays(bytes data) public {
// byte arrays ("bytes") are different as they are stored without padding,
// but can be treated identical to "uint8[]"
m_byteData = data;
m_byteData.length += 7;
m_byteData[3] = byte(8);
delete m_byteData[2];
}
function addFlag(bool[2] flag) public returns (uint) {
return m_pairsOfFlags.push(flag);
}
function createMemoryArray(uint size) public pure returns (bytes) {
// Dynamic memory arrays are created using `new`:
uint[2][] memory arrayOfPairs = new uint[2][](size);
// Create a dynamic byte array:
bytes memory b = new bytes(200);
for (uint i = 0; i < b.length; i++)
b[i] = byte(i);
return b;
}
}
参考:
https://solidity.readthedocs.io/en/develop/types.html#arrays