Smart Contract Language Solidity Tutorial Series 5-Introduction to arrays

Source: Internet
Author: User
Tags array length

Solidity Tutorial Series 5th-Introduction to solidity arrays.
For a complete list of articles in the solidity series, see Category-solidity.

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 part 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.

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:

pragma solidity ^0.4.16;contract C {    function f(uint len) public pure {        uint[] memory a = new uint[](7);        //a.length = 100;  // 错误        bytes memory b = new bytes(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:

pragma solidity ^0.4.16;contract C {    function f() public pure {        g([uint(1), 2, 3]);    }    function g(uint[3] _data) public pure {        // ...    }}

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:

//  无法编译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[] x = [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.

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

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**20] 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 functions setallflagpairs (bool[2][] newpairs) public {m_pairsofflags = Newpairs; } function Setflagpair (UINT index, BOOL Flaga, BOOL FLAGB) public {//access non-existent index throws an exception m_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 = new    Size;        } function Clear () public {//destroy delete m_pairsofflags;        Delete m_alotofintegers;    Same effect as Destroy 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);    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 is 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; }}
Reference documents

Solidity official documents-arrays

? In-depth blockchain-the system learns blockchain to create the best blockchain technology blog.

Smart Contract Language Solidity Tutorial Series 5-Introduction to arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.