动态二维数组定义

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;
}
  • 本文链接: https://dreamerjonson.com/2018/11/20/solidity-20-dynamictwoarray/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

solidity智能合约[20]-动态二维数组_第1张图片