【转载】solidity智能合约[20]-动态二维数组

solidity

#1

动态二维数组定义

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

获取长度

function getLength() view public returns(uint){

     return grade.length;
 }

   function getLength2() view public returns(uint){

     return grade[0].length;
 }

修改长度

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;
  }

添加修改内容

function changeContent() public{
     grade[1][0] = 100;
 }

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

遍历内容

//获取内容并求和
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/>