美文网首页
以太坊(十四)Solidity数据类型-数组

以太坊(十四)Solidity数据类型-数组

作者: duanyu | 来源:发表于2018-03-20 13:36 被阅读397次

    目标

    • 掌握Arrays的可变不可变的创建
    • 理解可变数组和不可变数组之间的区别
    • 二位数组
    • memory arrays的创建
    • bytes0 ~ bytes32、bytes与byte[]对比

    固定长度的数组(Arrays)

    固定长度类型数组的声明

    pragma solidity ^0.4.4;
    
    contract C {
        // 数组的长度为5,数组里面的存储的值的类型为uint类型
        uint [5] T = [1,2,3,4,5];
    }
    
    

    通过length方法获取数组长度遍历数组求总和

    pragma solidity ^0.4.4;
    
    contract C {
    
        // 数组的长度为5,数组里面的存储的值的类型为uint类型
        uint [5] T = [1,2,3,4,5];
    
        // 通过for循环计算数组内部的值的总和
        function numbers() constant public returns (uint) {
            uint num = 0;
            for(uint i = 0; i < T.length; i++) {
                num = num + T[i];
            }
            return num;
        }
    
    }
    
    

    尝试修改T数组的长度

    pragma solidity ^0.4.4;
    
    contract C {
    
        uint [5] T = [1,2,3,4,5];
        function setTLength(uint len) public {
            //编译错误
            T.length = len;
        }
    
    }
    
    
     TypeError: Expression has to be an lvalue.
            T.length = len;
            ^------^
    

    声明数组时,一旦长度固定,将不能再修改数组的长度。

    尝试修改T数组内部值

    pragma solidity ^0.4.4;
    
    contract C {
    
        uint [5] T = [1,2,3,4,5];
    
        function setTIndex0Value() public {
            T[0] = 10;
        }
    
        // 通过for循环计算数组内部的值的总和
        function numbers() constant public returns (uint) {
            uint num = 0;
            for(uint i = 0; i < T.length; i++) {
                num = num + T[i];
            }
            return num;
        }
    
    }
    
    

    T数组初始的内容为[1,2,3,4,5],总和为15 ,当调用setTIndex0Value方法将第0个索引的1修改为10时,总和为24

    尝试通过push往T数组中添加值

    pragma solidity ^0.4.4;
    
    contract C {
    
        uint [5] T = [1,2,3,4,5];
    
        function pushUintToT() public {
            //编译错误
            T.push(6);
        }
    }
    
    
    TypeError: Member "push" not found or not visible after argument-dependent lookup in uint256[5] storage ref
            T.push(6);
            ^----^
    

    结论:固定大小的数组不能调用push方法向里面添加存储内容,声明一个固定长度的数组,我们可以通过索引修改里面的值,但是不可修改数组长度以及不可通过push添加存储内容,而bytes0 ~ bytes32固定大小字节数组中,大小固定,内容固定,长度和字节均不可修改。

    可变长度的Arrays

    可变长度类型数组的声明

    pragma solidity ^0.4.4;
    
    contract C {
    
        uint [] T = [1,2,3,4,5];
    
        function T_Length() constant returns (uint) {
    
            return T.length;
        }
    
    }
    
    

    uint [] T = [1,2,3,4,5],这句代码表示声明了一个可变长度的T数组,因为我们给它初始化了5个无符号整数,所以它的长度默认为5

    通过length方法获取数组长度遍历数组求总和

    pragma solidity ^0.4.4;
    
    contract C {
    
        uint [] T = [1,2,3,4,5];
    
        // 通过for循环计算数组内部的值的总和
        function numbers() constant public returns (uint) {
            uint num = 0;
            for(uint i = 0; i < T.length; i++) {
                num = num + T[i];
            }
            return num;
        }
    
    }
    
    

    尝试修改T数组的长度

    pragma solidity ^0.4.4;
    
    contract C {
    
        uint [] T = [1,2,3,4,5];
    
        function setTLength(uint len) public {
    
            T.length = len;
        }
    
        function TLength() constant returns (uint) {
    
            return T.length;
        }
    }
    
    

    对可变长度的数组而言,可随时通过length修改它的长度。

    尝试通过push往T数组中添加值

    pragma solidity ^0.4.4;
    
    contract C {
    
        uint [] T = [1,2,3,4,5];
    
        function T_Length() constant public returns (uint) {
    
            return T.length;
        }
    
        function pushUintToT() public {
    
            T.push(6);
        }
    
        function numbers() constant public returns (uint) {
            uint num = 0;
            for(uint i = 0; i < T.length; i++) {
                num = num + T[i];
            }
            return num;
        }
    }
    
    

    当往里面增加一个值,数组的个数就会加1,当求和时也会将新增的数字加起来。

    二维数组 - 数组里面放数组

    pragma solidity ^0.4.4;
    
    contract C {
    
        uint [2][3] T = [[1,2],[3,4],[5,6]];
    
        function T_len() constant public returns (uint) {
    
            return T.length; // 3
        }
    }
    
    

    uint [2][3] T = [[1,2],[3,4],[5,6]]这是一个三行两列的数组,你会发现和Java、C语言等的其它语言中二位数组里面的列和行之间的顺序刚好相反。在其它语言中,上面的内容应该是这么存储uint [2][3] T = [[1,2,3],[4,5,6]]

    上面的数组Tstorage类型的数组,对于storage类型的数组,数组里面可以存放任意类型的值(比如:其它数组,结构体,字典/映射等等)。对于memory类型的数组,如果它是一个public类型的函数的参数,那么它里面的内容不能是一个mapping(映射/字典),并且它必须是一个ABI类型。

    创建 Memory Arrays

    创建一个长度为lengthmemory类型的数组可以通过new关键字来创建。memory数组一旦创建,它不可通过length修改其长度。

    pragma solidity ^0.4.4;
    
    contract C {
    
        function f(uint len) {
            uint[] memory a = new uint[](7);
            bytes memory b = new bytes(len);
            // 在这段代码中 a.length == 7 、b.length == len
            a[6] = 8;
        }
    }
    
    

    数组字面量 Array Literals / 内联数组 Inline Arrays

    pragma solidity ^0.4.4;
    
    contract C {
    
        function f() public {
            //编译错误
            g([1, 2, 3]);
        }
    
        function g(uint[3] _data) public {
            // ...
        }
    }
    
    
     TypeError: Invalid type for argument in function call. Invalid implicit conversion from uint8[3] memory to uint256[3] memory requested.
            g([1, 2, 3]);
              ^-------^
    

    在上面的代码中,[1, 2, 3]uint8[3] memory 类型,因为1、2、3都是uint8类型,他们的个数为3,所以[1, 2, 3]uint8[3] memory 类型。但是在g函数中,参数类型为uint[3]类型,显然我们传入的数组类型不匹配,所以会报错。

    正确的写法如下:

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

    在这段代码中,我们将[1, 2, 3]里面的第0个参数的类型强制转换为uint类型,所以整个[uint(1), 2, 3]的类型就匹配了g函数中的uint[3]类型。

    memory类型的固定长度的数组不可直接赋值给storage/memory类型的可变数组

    • TypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[] memory.
    pragma solidity ^0.4.4;
    
    contract C {
        function f() public {
            //编译错误
            uint[] memory x = [uint(1), 3, 4];
        }
    }
    
    
    TypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[] memory.
            uint[] memory x = [uint(1), 3, 4];
            ^-------------------------------^
    
    
    • TypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[] storage pointer
    pragma solidity ^0.4.4;
    
    contract C {
        function f() public {
    
            uint[] storage x = [uint(1), 3, 4];
        }
    }
    
    
    TypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[] storage pointer.
            uint[] storage x = [uint(1), 3, 4];
            ^--------------------------------^
    
    
    • 正确使用
    pragma solidity ^0.4.4;
    
    contract C {
        function f() public {
            //用长度为3的固定数组声明
            uint[3] memory x = [uint(1), 3, 4];
        }
    }
    
    

    创建固定大小字节数组/可变大小字节数组

    之前我们的文章中深入讲解了bytes0 ~ bytes32bytes以及string的使用。bytes0 ~ bytes32创建的是固定字节大小的字节数组,长度不可变,内容不可修改。而string是特殊的可变字节数组,它可以转换为bytes以通过length获取它的字节长度,亦可通过索引修改相对应的字节内容。

    创建可变字节数组除了可以通过bytes b = new bytes(len)来创建外,我们亦可以通过byte[] b来进行声明。

    bytes0 ~ bytes32我们可以通过byte[len] b来创建,len 的范围为0 ~ 32。不过这两种方式创建的不可变字节数组有一小点区别,bytes0 ~ bytes32直接声明的不可变字节数组中,长度不可变,内容不可修改。而byte[len] b创建的字节数组中,长度不可变,但是内容可修改

    pragma solidity ^0.4.4;
    
    contract C {
    
     pragma solidity ^0.4.4;
    
    contract C {
    
        bytes10 a = 0x77616e6773616e6a756e;
        byte[10] aa = [byte(0x77),0x61,0x6e,0x67,0x73,0x61,0x6e,0x6a,0x75,0x6e];
    
        byte[] cc = new byte[](3);
    
        function setAIndex0Byte() public {
            // 错误,不可修改
            a[0] = "L";
        }
    
        function setAAIndex0Byte() public {
    
            aa[0] = "L";
        }
    
        function setCC() public {
    
            for(uint i = 0; i < a.length; i++) {
                cc.push(a[i]);
            }
        }
    
    }
    
    

    总结

    本篇文章系统讲解了可变与不可变数组的创建、以及二位数组与其它语言中二位数组的区别,同时讲解了如何创建memory类型的数组以及对bytes0 ~ bytes32、bytes与byte[]对比分析。

    相关文章

      网友评论

          本文标题:以太坊(十四)Solidity数据类型-数组

          本文链接:https://www.haomeiwen.com/subject/mfgpqftx.html