Code has been added to clipboard!

Solidity Reference Types Example 5

Example
pragma solidity ^0.4.0;

contract contractArray {
    uint[2**20] m_IntegersLots;
    // Keep in mind that the following is not a pair of dynamic sized arrays but a
    // dynamic array filled with pairs instead (for example, of fixed size arrays of length two).
    bool[2][] m_FlagPairs;
    // newPairs will be stored inside memory - the default used for the function arguments' data location

    function setAllFlagPairs(bool[2][] newPairs) {
        // assigning to a storage array will replace the whole array
        m_FlagPairs = newPairs;
    }

    function setPairOfFlags(uint index, bool flagA, bool flagB) {
        // access to a non-existing index will throw an exception
        m_FlagPairs[index][0] = flagA;
        m_FlagPairs[index][1] = flagB;
    }

    function modifyFlagArraySize(uint newSize) {
        // if the newly assigned size is smaller, arrays that we removed, will be cleared
        m_FlagPairs.length = newSize;
    }

    function clearArray() {
        // arrays get completely cleared
        delete m_FlagPairs;
        delete m_IntegersLots;
        // same effect using this line
        m_FlagPairs.length = 0;
    }

    bytes m_bytesData;

    function bytesArrays(bytes data) {
        // byte arrays ("bytes") are not the same since they do not get stored with padding,
        // but may be treated identically to "uint8[]"
        m_bytesData = data;
        m_bytesData.length += 7;
        m_bytesData[3] = 8;
        delete m_bytesData[2];
    }

    function addFlag(bool[2] flag) returns (uint) {
        return m_FlagPairs.push(flag);
    }

    function generateMemoryArray(uint size) returns (bytes) {
        // Dynamic memory arrays are generated using `new`:
        uint[2][] memory pairArray = new uint[2][](size);
        // generates a dynamic sized byte array:
        bytes memory b = new bytes(200);
        for (uint i = 0; i < b.length; i++)
            b[i] = byte(i);
        return b;
    }
}