solidity智能合约[18]-twoarray
原创
©著作权归作者所有:来自51CTO博客作者gopher梦工厂的原创作品,请联系作者获取转载授权,否则将追究法律责任
二维数组定义
1
| uint[2][3] grade = [[60,80],[40,20],[50,50]];
|
返回二维数组
1 2 3 4
| function getGrade() view public returns(uint[2][3]){
return grade; }
|
获取二维数组长度
1 2 3 4 5 6 7 8 9
| function getLength() view public returns(uint){
return grade.length; }
function getLength2() view public returns(uint){
return grade[0].length; }
|
改变内容
1 2 3 4 5 6 7
| function changeContent() public {
grade[0][1] = 100;
// grade.push(ericGrade); // grade[0][2]=90; }
|
完整代码演示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| pragma solidity ^0.4.23;
contract twoArray{
uint[2] ericGrade = [40,20];
uint[2][3] grade = [[60,80],[40,20],[50,50]];
function getGrade() view public returns(uint[2][3]){
return grade; }
function getLength() view public returns(uint){
return grade.length; }
function getLength2() view public returns(uint){
return grade[0].length; }
function changeContent() public {
grade[0][1] = 100;
}
function add() view public returns(uint){ uint sum = 0; for(uint i = 0;i<3;i++){ for(uint j = 0;j<2;j++){ sum+= grade[i][j]; } } return sum; }
}
|