In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the use of Solidit array", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "what is the use of Solidit array" bar!
Array (Arrays)
The array can be declared with a specified length or dynamically lengthened. For arrays stored by storage, the element type can be arbitrary, and the type can be array, mapping type, structure, and so on. But for memory's array. As an argument to the public function, it cannot be an array of mapped types, it can only be a type that supports ABI.
An array of element type T and fixed length k can be declared T [k], while an array of dynamic size (variable length) can be declared T []. You can also declare a multidimensional array, such as a variable length array of type uint with length 5 (all 5 elements are variable length arrays), which can be declared uint [] [5]. Note that the length declaration of a multidimensional array is inverse compared to a non-blockchain language. )
To access the second element of the third dynamic array, use x [2] [1]. The sequence number of the array starts at 0, and the order of the sequence number is opposite to the definition.
Bytes and string are special arrays. Bytes is similar to byte [], but bytes is compressed when an external function is called as a parameter. String is similar to bytes, but does not provide length and serial number access (currently). So try to use bytes instead of byte [].
The string s can be converted into a bytes through bytes (s), the length can be obtained by bytes (s) .length, and the corresponding UTF-8 code can be obtained by bytes (s) [n]. What is obtained through subscript access is not the corresponding character, but the UTF-8 code, for example, the Chinese code is multi-byte and long, so the subscript accesses only one of the codes. A state variable of type array, which can be marked as public, allowing Solidity to create an accessor and specify a numeric subscript if you want to access an element of the array. (code example later)
Create a memory array
You can create an array of memory using the new keyword. Unlike the stroage array, you cannot modify the array size property through the length of .length. Let's look at the following example:
Pragma solidity ^ 0.4.16 X contract C {function f (uint len) public pure {uint [] memory a = new uint [] (7); / a.length = 100; / / error bytes memory b = new bytes (len); / / Here we have a.length = = 7 and b.length = = len a [6] = 8;}} array constant and inline array
Array constant, which is an array expression (not yet assigned to a variable). Here is a simple example:
Pragma solidity ^ 0.4.16 X contract C {function f () public pure {g ([uint (1), 2,3]);} function g (uint [3] _ data) public pure {/ /.}}
Through array constants, the array created is memory and fixed-length. An element type is an available type that uses an element that happens to be stored, such as [1, 2, 3], which can be stored only by uint8, and its type is uint8 [3] memory.
Because the argument to the g () method requires uint (the default uint represents uint256), you need to type the first element, using uint (1) to do this conversion.
It is also important to note that fixed-length arrays cannot be assigned to each other with variable-length arrays, so let's look at the following code:
/ / unable to compile pragma solidity ^ 0.4.0X 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];}}
It is planned to remove such restrictions in the future. At present, there are still some problems because of ABI passing arrays.
Member length attribute
The array has a .length property that represents the current array length. Storage's variable-length array, which can be adjusted by assigning a value to .length. Memory's variable length array is not supported. You cannot automatically change the length of an array by accessing it beyond the length of the current array. Although the size of the memory array can be flexibly specified through parameters, once created, the size cannot be adjusted.
Push method
Both storage's variable-length array and bytes have a push method (string does not) that appends a new element to the end of the data and returns a new length.
Restricted situation
Currently, you cannot use multidimensional arrays in external functions.
In addition, due to the limitations of EVM, dynamic content cannot be returned through external functions.
Contract C {function f () returns (uint []) {...}}
In this example, the data can be returned through the web.js call, but not from the Solidity call. One way to get around this problem is to use a very large static array.
Pragma solidity ^ 0.4.16 leading contract ArrayContract {uint [2 dynamic arrays 20] masks LotOf Integers; / / this is not an array of two dynamic arrays, but a dynamic array in which each element is an array of two in length. Bool [2] [] mairsOfFlags; / / 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 {/ / accesses a nonexistent index and throws an exception m _ pairsOfFlags [index] [0] = flagA; mairsOfFlags [index] [1] = flagB } function changeFlagArraySize (uint newSize) public {/ / if the new size is smaller, the removed elements will be destroyed m_pairsOfFlags.length = newSize;} function clear () public {/ / destroy delete delete LotOfIntegers; / / the same effect as destroy m_pairsOfFlags.length = 0;} bytes m_byteData Function byteArrays (bytes data) public {/ / byte arrays ("bytes") are different as they are stored without padding, / / but can be 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 are created using `new`: uint [2] [] memory arrayOfPairs = new uint [2] [] (size); / / Create a dynamic byte array: bytes memory b = new bytes Thank you for reading, above is the content of "what is the use of Solidit array". After the study of this article, I believe you have a deeper understanding of what the use of Solidit array is, and the specific use of Solidit array still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.