固定長度的數組(Arrays)
固定長度類型數組的聲明
pragma solidity ^0.4.4;
contract C {
// 數組的長度爲5,數組裏面的存儲的值的類型爲uint類型
uint [5] T = [1,2,3,4,5];
}
通過length方法獲取數組長度遍歷數組求總和
pragma solidity ^0.4.4;
contract C {
// 數組的長度爲5,數組裏面的存儲的值的類型爲uint類型
uint [5] T = [1,2,3,4,5];
// 通過for循環計算數組內部的值的總和
function numbers() constant public returns (uint) {
uint num = 0;
for(uint i = 0; i < T.length; i++) {
num = num + T[i];
}
return num;
}
}
聲明數組時,一旦長度固定,將不能再修改數組的長度。
嘗試修改T數組內部值
pragma solidity ^0.4.4;
contract C {
uint [5] T = [1,2,3,4,5];
function setTIndex0Value() public {
T[0] = 10;
}
// 通過for循環計算數組內部的值的總和
function numbers() constant public returns (uint) {
uint num = 0;
for(uint i = 0; i < T.length; i++) {
num = num + T[i];
}
return num;
}
}
通過一個簡單的試驗可證明固定長度的數組只是不可修改它的長度,不過可以修改它內部的值
,而bytes0 ~ bytes32
固定大小字節數組中,大小固定,內容固定,長度和字節均不可修改
。
嘗試通過push往T數組中添加值
pragma solidity ^0.4.4;
contract C {
uint [5] T = [1,2,3,4,5];
function pushUintToT() public {
T.push(6);
}
}
固定大小的數組不能調用push方法
向裏面添加存儲內容,聲明一個固定長度的數組,比如:uint [5] T
,數組裏面的默認值爲[0,0,0,0,0]
,我們可以通過索引修改裏面的值,但是不可修改數組長度以及不可通過push添加存儲內容
。
可變長度的Arrays
可變長度類型數組的聲明
pragma solidity ^0.4.4;
contract C {
uint [] T = [1,2,3,4,5];
function T_Length() constant returns (uint) {
return T.length;
}
}
uint [] T = [1,2,3,4,5]
,這句代碼表示聲明瞭一個可變長度的T數組
,因爲我們給它初始化了5個無符號整數,所以它的長度默認爲5。
通過length方法獲取數組長度遍歷數組求總和
pragma solidity ^0.4.4;
contract C {
uint [] T = [1,2,3,4,5];
// 通過for循環計算數組內部的值的總和
function numbers() constant public returns (uint) {
uint num = 0;
for(uint i = 0; i < T.length; i++) {
num = num + T[i];
}
return num;
}
}
嘗試修改T數組的長度
pragma solidity ^0.4.4;
contract C {
uint [] T = [1,2,3,4,5];
function setTLength(uint len) public {
T.length = len;
}
function TLength() constant returns (uint) {
return T.length;
}
}
對可變長度的數組而言,可隨時通過length修改它的長度。
嘗試通過push往T數組中添加值
pragma solidity ^0.4.4;
contract C {
uint [] T = [1,2,3,4,5];
function T_Length() constant public returns (uint) {
return T.length;
}
function pushUintToT() public {
T.push(6);
}
function numbers() constant public returns (uint) {
uint num = 0;
for(uint i = 0; i < T.length; i++) {
num = num + T[i];
}
return num;
}
}
當往裏面增加一個值,數組的個數就會加1,當求和時也會將新增的數字加起來。
二維數組 - 數組裏面放數組
pragma solidity ^0.4.4;
contract C {
uint [2][3] T = [[1,2],[3,4],[5,6]];
function T_len() constant public returns (uint) {
return T.length; // 3
}
}
uint [2][3] T = [[1,2],[3,4],[5,6]]
這是一個三行兩列的數組,你會發現和Java、C語言等
的其它語言中二位數組裏面的列和行之間的順序剛好相反
。在其它語言中,上面的內容應該是這麼存儲uint [2][3] T = [[1,2,3],[4,5,6]]
。
上面的數組T是storage類型的數組,對於storage
類型的數組,數組裏面可以存放任意類型的值(比如:其它數組,結構體,字典/映射等等)
。對於memory
類型的數組,如果它是一個public
類型的函數的參數,那麼它裏面的內容不能是一個mapping(映射/字典)
,並且它必須是
一個ABI
類型。
創建 Memory Arrays
創建一個長度爲length
的memory
類型的數組可以通過new
關鍵字來創建。memory
數組一旦創建,它不可通過length修改其長度
。
pragma solidity ^0.4.4;
contract C {
function f(uint len) {
uint[] memory a = new uint[](7);
bytes memory b = new bytes(len);
// 在這段代碼中 a.length == 7 、b.length == len
a[6] = 8;
}
}
數組字面量 Array Literals / 內聯數組 Inline Arrays
pragma solidity ^0.4.4;
contract C {
function f() public {
g([1, 2, 3]);
}
function g(uint[3] _data) public {
// ...
}
}
在上面的代碼中,[1, 2, 3]
是 uint8[3] memory
類型,因爲1、2、3
都是uint8
類型,他們的個數爲3
,所以[1, 2, 3]
是 uint8[3] memory
類型。但是在g
函數中,參數類型爲uint[3]
類型,顯然我們傳入的數組類型不匹配,所以會報錯
。
正確的寫法如下:
pragma solidity ^0.4.4;
contract C {
function f() public {
g([uint(1), 2, 3]);
}
function g(uint[3] _data) public {
// ...
}
}
在這段代碼中,我們將[1, 2, 3]
裏面的第0個參數的類型強制轉換爲uint類型
,所以整個[uint(1), 2, 3]的類型就匹配了g函數中的uint[3]類型
。
memory類型的固定長度的數組不可直接賦值給storage/memory類型的可變數組
- TypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[] memory.
pragma solidity ^0.4.4;
contract C {
function f() public {
uint[] memory x = [uint(1), 3, 4];
}
}
browser/ballot.sol:8:9: TypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[] memory.
uint[] memory x = [uint(1), 3, 4];
^-------------------------------^
- TypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[] storage pointer
pragma solidity ^0.4.4;
contract C {
function f() public {
uint[] storage x = [uint(1), 3, 4];
}
}
browser/ballot.sol:8:9: TypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[] storage pointer.
uint[] storage x = [uint(1), 3, 4];
^--------------------------------^
- 正確使用
pragma solidity ^0.4.4;
contract C {
function f() public {
uint[3] memory x = [uint(1), 3, 4];
}
}
創建固定大小字節數組/可變大小字節數組
bytes0 ~ bytes32
創建的是固定字節大小的字節數組
,長度不可變,內容不可修改
。而string
是特殊的可變字節數組
,它可以轉換爲bytes以通過length
獲取它的字節長度
,亦可通過索引修改
相對應的字節內容
。
創建可變字節數組除了可以通過bytes b = new bytes(len)
來創建外,我們亦可以通過byte[] b
來進行聲明。
而bytes0 ~ bytes32
我們可以通過byte[len] b
來創建,len
的範圍爲0 ~ 32
。不過這兩種方式創建的不可變字節數組有一小點區別,bytes0 ~ bytes32
直接聲明的不可變字節數組中,長度不可變,內容不可修改
。而byte[len] b
創建的字節數組中,長度不可變,但是內容可修改。
pragma solidity ^0.4.4;
contract C {
bytes9 a = 0x6c697975656368756e;
byte[9] aa = [byte(0x6c),0x69,0x79,0x75,0x65,0x63,0x68,0x75,0x6e];
byte[] cc = new byte[](10);
function setAIndex0Byte() public {
// 錯誤,不可修改
a[0] = 0x89;
}
function setAAIndex0Byte() public {
aa[0] = 0x89;
}
function setCC() public {
for(uint i = 0; i < a.length; i++) {
cc.push(a[i]);
}
}
}