27

How to create arrays addresses using solidity? And we can write two-dimensional array?

bool[2][] m_pairsOfFlags;

Here is an example of the array, but it does not work

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
M.Sheremain
  • 511
  • 2
  • 6
  • 10

1 Answers1

31

Yes you can.

This little snippet might help. It's just a little toy to show how the two-dimensional array elements are referenced.

It might be helpful to point out that things may be a little counter-intuitive because the X & Y axis may seem to be reversed.

bool[2][] flags; 

It starts by describing a length 2 array of bools. Then it goes on to say there will be a dynamic list of those called flags.

So, to reference such things, the dynamic dimension is high order.

bool flag = flags[dynamicIndex][lengthTwoIndex];

The above might look backwards, so that can be disorienting.

Some functions I put together to demo this in Remix:

pragma solidity ^0.4.6;

// set all the inserts to true for simplicity

contract Arrays{

// dynamic list of length 2 bools
bool[2][] flags;

function Arrays() {
    // append a length 2 array to the dynamic list of flags
    flags.push([true,true]);
}

function appendFlag() returns(uint length) {
   // append another length 2 array to the dynamic dimension
   return flags.push([true,true]);
}

// return a length 2 array stored in the dynamic dimension
// will throw if index > flags.length-1 (index starts at 0)
function getFlags(uint index) constant returns(bool[2] flagList) {
    return(flags[index]);
}

// return one flag from the array
function getFlag(uint dynamicIndex, uint lengthTwoIndex) constant returns(bool flag) {
    return flags[dynamicIndex][lengthTwoIndex];
}

// return a count of length 2 arrays stored in flags;
function getFlagsCount() constant returns(uint count) {
    return flags.length;
}

}

enter image description here

Hope it helps.

Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143
Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145