TExcelTAD
Home › Solidity › Arrays

Arrays

Solidity has fixed-size arrays (uint256[3]) and dynamic arrays (uint256[]). In storage, dynamic arrays can grow and shrink with push and pop; in memory, every array has a fixed length once created.

Storage arrays

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    uint256[] public queue;            // dynamic, starts empty
    uint256[3] public podium;          // fixed: always 3 items, all 0

    function main() external {
        queue.push(42);
        queue.push(7);
        queue.push(19);
        console.log("length after 3 pushes:", queue.length);
        console.log("first and last:", queue[0], queue[queue.length - 1]);

        queue.pop();                   // removes the last item
        console.log("length after pop:", queue.length);

        queue[0] = 100;                // overwrite by index
        console.log("queue[0] now:", queue[0]);

        podium[0] = 1;
        console.log("podium length:", podium.length, "podium[2]:", podium[2]);
    }
}
length after 3 pushes: 3
first and last: 42 19
length after pop: 2
queue[0] now: 100
podium length: 3 podium[2]: 0

The public getter of an array takes an index: queue(0) from outside returns one element. To return the whole array, write a function that returns it as memory.

Out-of-bounds access

Reading or writing past the end reverts with a panic (code 0x32), and so does pop() on an empty array (code 0x31):

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    uint256[] public items;

    function main() external {
        items.push(1);
        console.log("items[0] =", items[0]);
        console.log("reading items[5]...");
        console.log(items[5]);
    }
}
items[0] = 1
reading items[5]...
main() failed: panic 0x32 (array index out of bounds)

Memory arrays

In memory you create arrays with new T[](n) or with an array literal. Literals have the type of their smallest element type, so [1, 2, 3] is a uint8[3]; convert the first element to get the type you need:

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    function evens(uint256 count) internal pure returns (uint256[] memory result) {
        result = new uint256[](count);
        for (uint256 i = 0; i < count; i++) {
            result[i] = i * 2;
        }
    }

    function main() external pure {
        uint256[] memory e = evens(5);
        console.log("length", e.length, "last", e[4]);

        uint256[3] memory primes = [uint256(2), 3, 5];
        console.log("primes:", primes[0], primes[1], primes[2]);

        string[2] memory names = ["Ada", "Linus"];
        console.log("names:", names[0], names[1]);
    }
}
length 5 last 8
primes: 2 3 5
names: Ada Linus

Removing an element

There is no built-in "remove at index". delete arr[i] only resets the element to zero; the length stays the same. Two common patterns:

  1. Swap and pop (fast, O(1), but changes the order): copy the last element into the gap, then pop().
  2. Shift left (keeps order, costs gas proportional to the length).
solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Main {
    uint256[] public ids;

    function show(string memory label) internal view {
        console.log(label, ids.length > 0 ? ids[0] : 0, ids.length > 1 ? ids[1] : 0, ids.length);
    }

    function removeUnordered(uint256 index) internal {
        ids[index] = ids[ids.length - 1];
        ids.pop();
    }

    function main() external {
        ids = [uint256(11), 22, 33];
        delete ids[0];
        show("after delete (first, second, length):");

        ids = [uint256(11), 22, 33];
        removeUnordered(0);
        show("after swap-and-pop (first, second, length):");
    }
}
after delete (first, second, length): 0 22 3
after swap-and-pop (first, second, length): 33 22 2

Assigning a memory array literal to a storage array (ids = [...]) replaces its whole contents.

Returning arrays and multi-dimensional arrays

solidity · try it yourself
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "hardhat/console.sol";

contract Board {
    uint8[3][3] public cells;          // 3 rows of 3 cells

    function play(uint256 row, uint256 col, uint8 player) external {
        require(cells[row][col] == 0, "cell taken");
        cells[row][col] = player;
    }

    function row(uint256 r) external view returns (uint8[3] memory) {
        return cells[r];
    }
}

contract Main {
    function main() external returns (uint8[3] memory) {
        Board board = new Board();
        board.play(1, 0, 1);
        board.play(1, 1, 2);
        board.play(1, 2, 1);
        console.log("cell (1,1):", board.cells(1, 1));
        return board.row(1);
    }
}
cell (1,1): 2
main() returned: [1, 2, 1]

Note: Solidity reads multi-dimensional types "backwards" compared with C: uint8[3][5] is an array of 5 elements, each a uint8[3]. Indexing works left to right as you'd expect: cells[row][col].

Gas considerations

  • Every push to a storage array writes at least one new slot, roughly 20,000+ gas.
  • Iterating over a storage array reads every slot; keep arrays bounded or use mappings.
  • Returning a large array from a view function is fine for off-chain readers, but other contracts calling it pay for all the copying.

Test yourself

What does delete arr[2] do to a storage array of length 5?

  1. Removes the element; length becomes 4
  2. Sets the element to its zero value; length stays 5
  3. Deletes the whole array
  4. Reverts

Which operation is NOT available on a uint256[] memory array?

  1. Reading .length
  2. Writing a[0] = 1
  3. a.push(1)
  4. Returning it from a function

What happens when you read items[10] on an array with 3 elements?

  1. Returns 0
  2. Returns garbage data
  3. Reverts with a panic (index out of bounds)
  4. Grows the array