solidity智能合约[20]-动态二维数组

动态二维数组定义

uint[][] public grade = [[60,80],[40,20],[50,50]];

获取长度

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
8
9
10
11
12
13
14
15
16
function changeLength() public{

grade.length = 4;

}

function changeLength2() public{
grade.length =2;
}

function changeLength3() public{
grade[0].length =3;
}
function changeLength4() public{
grade[0].length =1;
}

添加修改内容

1
2
3
4
5
6
7
function changeContent() public{
grade[1][0] = 100;
}

function changeContent2() public{
grade.push([100,90]);
}

遍历内容

1
2
3
4
5
6
7
8
9
10
//获取内容并求和
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;
}