Intelligent Contract Language Solidity Tutorial Series 5-Array introduction __ block chain

Source: Internet
Author: User
Tags array length
It 's written in front .

Solidity is an intelligent contract programming language for Ethernet, you should have a better understanding of Ethernet and intelligent contracts before reading this article,
If you don't understand it, I suggest you look at the etheric square first.

The first half of this article is a reference to the official document of solidity (current latest version: 0.4.20) for translation, and the latter part of the official document does not provide code knowledge points Supplemental Code instructions (subscribe to the column read). Array (Arrays)

An array can declare a length, or it can be dynamically longer. For storage stored arrays, element types can be arbitrary, types can be arrays, mapping types, structs, and so on. But for an array of memory. If it is an argument to the public function, it cannot be an array of mapped types, only the type that supports the ABI.

An array of element type T, fixed length k, can be declared as t[k], while 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 a type of uint of 5 (5 elements are variable length arrays), which can be declared as uint[][5]. (Note that the length declaration of a multidimensional array is reversed compared to a non-block chain language.) )

To access the second element of the third dynamic array, use x[2][1]. The ordinal of an array starts at 0, and the ordinal order is the opposite of the definition.

Bytes and string are special arrays. Bytes is similar to byte[], but bytes is packaged in a call to an external function as a parameter. A string is similar to bytes, but does not provide a length and an ordinal access mode (currently).
So you should try to use bytes instead of byte[].

The string s can be converted to a bytes through bytes (s), and can be obtained by bytes (s). Length, bytes (s) [n] gets the corresponding UTF-8 encoding. By subscript access is not the corresponding character, but UTF-8 encoding, for example, the Chinese encoding is multibyte, the longer, so the subscript access is only one of the code.
The type is an array of state variables that can be marked public, allowing solidity to create an accessor, and specifying a number subscript if you want to access an element of the array. (a later code case) to create 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 by 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 = m;  Error
        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, 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 created array is memory, and is 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 is uint8 to store, and its type is uint8[3] memory.

Because the parameters of the G () method require UINT (the default uint is actually uint256), you need to type the first element and use UINT (1) to do the conversion.

Also note that fixed-length arrays cannot be assigned to variable-length arrays, so let's look at the following code:

  unable to compile
pragma solidity ^0.4.0;

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

It has been planned to remove such restrictions in the future. There are some problems with the ABI passing array at this point. Members Length Property

The array has a. Length property that represents the current array length. A variable-length array of storage that adjusts the length of an array by assigning a value to. length. Memory variable-length arrays are not supported.
You cannot automatically change the length of an array by accessing the length beyond the current array. Although the memory array can be flexibly specified by its parameters, the size is not adjustable once it is created. Push Method

Storage variable-length arrays and bytes have a push method (string not) that attaches the new element to the end of the data, and the return value is the new length. Limiting situation

You cannot use multidimensional arrays currently in the external function.

In addition, based on EVM restrictions, dynamic content cannot be returned through external functions.

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

In this example, if the data can be returned through a web.js call, the call from solidity cannot return the 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 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;
    }

    

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.