# 数组

`数组`可以声明时指定长度，或者是变长的。对`storage`1的数组来说，元素类型可以是任意的，类型可以是数组，映射类型，数据结构等。但对于`memory`[datalocation]的数组来说。如果函数是对外可见的2，那么函数参数不能是映射类型的数组，只能是支持ABI的类型3

`bytes``string`是一种特殊的数组。`bytes`类似`byte[]`，但在外部函数作为参数调用中，会进行压缩打包，更省空间，所以应该尽量使用`bytes`4`string`类似`bytes`，但不提供长度和按序号的访问方式。

## 创建一个数组

``````pragma solidity ^0.4.0;

contract C {
function f() {
//创建一个memory的数组
uint[] memory a = new uint[](7);

//不能修改长度
//Error: Expression has to be an lvalue.
//a.length = 100;
}

//storage
uint[] b;

function g(){
b = new uint[](7);
//可以修改storage的数组
b.length = 10;
b = 100;
}
}
``````

## 字面量及内联数组

``````pragma solidity ^0.4.0;

contract C {
function f() {
g([uint(1), 2, 3]);
}
function g(uint _data) {
// ...
}
}
``````

``````pragma solidity ^0.4.0;

contract C {
function f() {
// The next line creates a type error because uint memory
// cannot be converted to uint[] memory.
uint[] x = [uint(1), 3, 4];
}
``````

## 数组的属性和方法

### push方法

`storage`的变长数组和`bytes`都有一个`push()`，用于附加新元素到数据末端，返回值为新的长度。

``````pragma solidity ^0.4.0;

contract C {
uint[] u;
bytes b;

function testArryPush() returns (uint){
uint memory a = [uint(1), 2, 3];

u = a;

return u.push(4);
}

function testBytesPush() returns (uint){
b = new bytes(3);
return b.push(4);
}
}
``````

### 限制的情况

``````pragma solidity ^0.4.0;

contract C {
function f() returns (uint[]) {
}
}
``````

``````pragma solidity ^0.4.0;

contract ArrayContract {
//the orginal length of m_aLotOfIntegers is 2**20
//run it cause a out of gas,so change it to a much smaller 2**2 for test
uint[2**2] m_aLotOfIntegers;
// Note that the following is not a pair of arrays but an array of pairs.
bool[] m_pairsOfFlags;
// newPairs is stored in memory - the default for function arguments

function setAllFlagPairs(bool[] newPairs) {
// assignment to a storage array replaces the complete array
m_pairsOfFlags = newPairs;
}

function setFlagPair(uint index, bool flagA, bool flagB) {
m_pairsOfFlags[index] = flagA;
m_pairsOfFlags[index] = flagB;
}

function changeFlagArraySize(uint newSize) {
// if the new size is smaller, removed array elements will be cleared
m_pairsOfFlags.length = newSize;
}

function clear() {
// these clear the arrays completely
delete m_pairsOfFlags;
delete m_aLotOfIntegers;
// identical effect here
m_pairsOfFlags.length = 0;
}

function addFlag(bool flag) returns (uint) {
return m_pairsOfFlags.push(flag);
}

function createMemoryArray(uint size)  {
// Dynamic memory arrays are created using `new`:
bool[] memory arrayOfPairs = new bool[](size);
m_pairsOfFlags = arrayOfPairs;
}
}
``````
{"ft_lang_label":"__label__zh","ft_lang_prob":0.6036655,"math_prob":0.9391309,"size":4382,"snap":"2022-27-2022-33","text_gpt3_token_len":2018,"char_repetition_ratio":0.12105984,"word_repetition_ratio":0.053117782,"special_character_ratio":0.25992697,"punctuation_ratio":0.14556041,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9628643,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-02T11:53:42Z\",\"WARC-Record-ID\":\"<urn:uuid:f4d33f54-4dfb-4394-a9c0-7e6e3a290772>\",\"Content-Length\":\"37169\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ac34ce80-0766-4d94-857a-2419264a439b>\",\"WARC-Concurrent-To\":\"<urn:uuid:35da1342-7917-4530-adcc-f55c3925adf0>\",\"WARC-IP-Address\":\"172.105.224.232\",\"WARC-Target-URI\":\"https://www.tryblockchain.org/Solidity-Array-%E6%95%B0%E7%BB%84.html\",\"WARC-Payload-Digest\":\"sha1:FJRNHQWUCYW7MNQFQBYNYT26N4BAFESA\",\"WARC-Block-Digest\":\"sha1:ZPWYK7GMFRTPVBUDMGZNCCBOQYH5NDYT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104054564.59_warc_CC-MAIN-20220702101738-20220702131738-00673.warc.gz\"}"}