3

I believe it's possible to create a two-dimensional array according to this answer.

I am trying to create a two-dimensional array to store the address and ether of each incoming transaction.

Are two-dimensional arrays the best solution in this case? Do we have to set a length limit or can we make it infinitely long? How can we prevent malicious users from flooding the array?

1 Answer 1

6

When declaring a two-dimensional array in storage you are creating in reality nested arrays. And you do not need to declare sizes.

pragma solidity ^0.4.18;

contract C {

    // data[n] is an array of uints
    uint[][] data;

    function append(uint _a, uint _b) public {
        data.push([_a, _b]);
    }

    function read(uint _idx) public view returns (uint[]) {
        return data[_idx];
    }
}

Without more details about your problem is hard to recommend a particular solution.

  • Arrays are more complex than mappings and can be slightly more expensive to use when adding a new element.

  • Deleting a single element in an array does not resize the array, and you will need to find out how to deal with such situations.

  • You can iterate an array but not a mapping. Also iterating a large array can cause out of gas errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.