Write in front
Solidity is the Ethereum Smart Contract programming language, you should know about Ethereum and smart contracts before reading this article.
If you don't understand, it's recommended that you look at Ethereum first.
The first half of this article is a reference to the solidity official document (currently the latest version: 0.4.20) for translation, the latter part of the official documentation does not provide code for the knowledge Point Supplement Code description (subscription column reading).
Array (Arrays)
An array can be declared with a specified length, or it can be dynamically variable. For an array of storage storage, the element type can be arbitrary, and the type can be an array, a mapping type, a struct, and so on. But for the memory array. If it is an argument to the public function, it cannot be an array of mapping types, only the type that supports the ABI.
An array with an element type of T, a fixed length of k, can be declared as T[k], and an array of dynamic size (variable length) is declared as t[].
You can also declare a multidimensional array, such as declaring a variable-length array with an array of type UINT 5 (5 elements are variable-length arrays), which can be declared as uint[][5]. (Note that the length declarations of multidimensional arrays are reversed compared to non-blockchain languages.) )
To access the second element of a third dynamic array, use x[2][1]. The ordinal of an array starts with 0, and the ordinal order is the opposite of the definition.
bytes and string are a special type of array. bytes similar to byte[], but in an external function as a parameter call,bytes is compressed and packaged. string is similar to bytes, but does not provide length and ordinal access (currently).
So try to use bytes instead of byte[].
You can convert the string s through bytes (s) to a bytes, which can be obtained by bytes (s). length, andbytes (s) [n] gets the corresponding UTF-8 encoding. The subscript access is not the corresponding character, but the UTF-8 encoding, such as the Chinese encoding is multibyte, variable length, so the subscript access to only one of the encoding.
A type is an array of state variables that can be marked as public, allowing solidity to create an accessor, and if you want to access an element of an array, specify a number subscript. (later code case)
Creating an array of memory
You can use the New keyword to create an array of memory. Unlike the stroage array, you cannot modify the array size property through the length of. Length. Let's take a look at the following example:
^0.4.16;{ functionfpublic{ =new uint[](7); //a.length = 100; // 错误 =newbytes(len); // Here we have a.length == 7 and b.length == len a[6=8; }}
Array constants and inline arrays
An array constant, which is an array expression (not yet assigned to a variable). The following is a simple example:
^0.4.16;{ functionfpublic{ g([uint(1),2,3]); } functiong(uint[3public{ // ... }}
With array constants, the array created is memory and is also fixed-length. An element type is a type of energy that uses just the elements that can be stored, such as [1, 2, 3], which only needs to be uint8 to be stored, and its type is uint8[3] memory.
Since the parameters of the G () method require a UINT (the default uint is actually uint256), the first element needs to be type-converted and UINT (1) used for this conversion.
It is also important to note that the fixed-length array cannot be assigned to each other with variable-length arrays, so let's look at the following code:
// 无法编译^0.4.0;{ functionfpublic{ // The next line creates a type error because uint[3] memory // cannot be converted to uint[] memory. = [uint(1),3,4]; }}
There are plans to remove such restrictions in the future. Currently there are some problems with the ABI passing array.
Member Length Property
An array has a. Length property that represents the current array length. A variable-length array of storage, which can be adjusted by assigning a. Length assignment to the array length. The variable-length array of memory is not supported.
It is not possible to automatically change the array length by accessing the length of the current array. Although the memory array can be flexibly specified by parameters, the size cannot be adjusted once it is created.
Push method
The storage array and bytes have a push method (string not) that appends the new element to the end of the data, and the return value is the new length.
Limitation of
You cannot use a multidimensional array currently in the external function.
Additionally, based on the EVM limit, dynamic content cannot be returned through an external function.
{ functionfreturns{} }
In this example, if the Web.js call can return data, but calls from solidity cannot return data. One way to circumvent this problem is to use a very large static array.
pragma solidity^0.4.16;Contract Arraycontract{uint[2** -] M_alotofintegers; //This is not an array of two dynamic arrays, but rather a dynamic array in which each element is an array of length two. bool[2[] M_pairsofflags; //Newpairs exists in memory because it is a function parameter function Setallflagpairs(bool[2] [] newpairs) Public {M_pairsofflags=Newpairs; } function Setflagpair(UINT Index,BOOL Flaga,BOOL FLAGB) Public { //access to non-existent index throws an exceptionm_pairsofflags[index][0]=Flaga;m_pairsofflags[index][1]=Flagb; } function changeflagarraysize(UINT newSize) Public { //If the new size is smaller, the removed element is destroyed M_pairsofflags.length =NewSize; } function Clear() Public { //Destroy DeleteM_pairsofflags; DeleteM_alotofintegers; //With the same effect as destruction M_pairsofflags.length = 0; }Bytes M_bytedata; function bytearrays(bytes data) Public { //byte arrays ("bytes") is different as they is stored without padding, //But can is treated identical to "uint8[]"M_bytedata=Data; M_bytedata.length += 7;m_bytedata[3]= byte(8); Deletem_bytedata[2]; } function Addflag(bool[2] flag) Public returns(UINT){ return M_pairsofflags.Push(flag); } function Creatememoryarray(UINT size) PublicPurereturns(bytes){ //Dynamic memory arrays is created using ' new ':uint[2[] Memory Arrayofpairs= Newuint[2[] (size); //Create a dynamic byte array:Bytes Memory B= New bytes( $); for(UINT I= 0;I< b.length;I++) B[i]= byte(i); returnB; }}
Supplementary case Description
For example code and explanation, please subscribe to blockchain technology to view.
Reference documents
Solidity official documents-arrays
Blockchain-Learn blockchain to build the best Blockchain technology blog
Smart Contract Language Solidity Tutorial Series 5-Introduction to arrays