pragma solidity ^0.4.0; contract test { uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 /* contract test { uint [] T = new uint[](5); //ParserError: Expected identifier but got 'storage' //全局数组,默认创建在storage中,无法创建在memory中,长度可修改 function setlength(uint aa){ uint [] memory a = new uint[](5); bytes [] memory b = new bytes[](5); //TypeError: Type bytes memory[] memory is not implicitly //convertible to expected type bytes storage ref[] storage pointer. //函数内创建数组,需要指定存储在memory中,a.length不可更改 //a.length = 10; // TypeError: Expression has to be an lvalue a[2] = 5; } } */ function setValue(uint para){ T[0] = para; } /* uint [] T =[1,2,3,4,5] ; //动态长度的数组:可修改数组内值大小,支持push,可更改长度 /* uint [] T1 = new uint[](5); constructor() public{ for (uint i;i<T1.length;i++){ T1[i] = i; } } */ function setlenth(uint para){ T.length = 6; } function addvalue(uint para){ T.push(3); } */ function setValue1(){ T[0] = 10; } function getValue() constant returns(uint){ return T.length; } function get1Value() constant returns(uint){ return T[0]; } } pragma solidity ^0.4.0; contract test { //二维数组 uint [2][3] T = [[1,2],[2,3]]; /* 1,2 2,3 0,0 */ uint [2][] T1 = new uint[2][](5); /*[i][j] T.length = j 0,0 0,0 0,0 0,0 0,0 */ function getlenth() constant returns(uint){ return T.length; //3 } function getlenth1() constant returns(uint){ return T1.length; //5 } function get1lenth() constant returns(uint[2][]){ //输出二维数组 return T1; //5 } } pragma solidity ^0.4.0; contract test { function setValue() public{ g([1,2,3]); //uint8 //TypeError: Invalid type for argument in function call. //Invalid implicit conversion from uint8[3] memory to uint256[3] memory requeste g([uint(1),2,3]); } //uint 256 function g(uint[3] data){ } uint [] x1 = [uint(1),2,3]; //storage:可变数组 memory:固定数组 function get1() public{ uint [] memory x = [uint(1),2,3]; //memory:可变数组 memory:固定数组 //在函数内部,memory类型的固定长度的数组不可直接赋值给storge/memory类型的可变数组 //TypeError: Invalid type for argument in function call. //Invalid implicit conversion from uint8[3] memory to uint256[3] memory requested } function get1() public constant returns(uint[]){ return x1; //uint256[]: 1,2,3 == [1,2,3] //uint [] memory x = [uint(1),2,3]; //TypeError: Invalid type for argument in function call. //Invalid implicit conversion from uint8[3] memory to uint256[3] memory requested } pragma solidity ^0.4.0; contract test { bytes3 public b = 0x123456; //bytes3: 0x123456 byte[3] public b1; //b1=0x000000 可直接通过索引进行查询 bytes public b2= new bytes(3); //bytes: 0x000000 == byte[] public b3 = new byte[](3) byte[] public b3 = new byte[](3); // 0x000000 可直接通过索引进行查询 /* function setb() public{ b[0] =0x01; } */ function setb1() public{ b1[0] =0x01; } function setb2(bytes aa)public { for (uint i;i<aa.length;i++){ b2.push(aa[i]); } } } /* 总结:创建固定大小字节数组/可变大小字节数组 固定大小字节数组: bytes0~bytes32:长度不可变,内容不可修改 byte[len] b :长度不可变,内容可以修改 可变大小字节数组:可直接通过索引进行查询 bytes b = new bytes(len) == byte[] b = new byte[](len) 特殊的可变字节数组: string :bytes() 通过bytes转换,length获取长度,通过索引修改相应的字节内容 固定大小字节数组 -> 可变大小字节数组 bytes3 a; bytes [] b = new bytes[a.length] for (uint i;i<a.length;i++){ b[i] = a[i] } return b; uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 uint [] T =[1,2,3,4,5] ; //动态长度的数组:可修改数组内值大小,支持push,可更改长度 uint [] T = new uint[](5); //ParserError: Expected identifier but got 'storage' //全局数组,默认创建在storage中,无法指定在memory中,长度可修改 //函数内创建数组,需要指定存储在memory中,a.length不可更改 uint [] x1 = [uint(1),2,3] //[uint(1),2,3] = [1,2,3]; //storage:可变数组 memory:固定数组 */ }