🎓 A1Learner
🔍
0 of 50 lessons completed

Solidity Changing Arrays

Changing an array means adding, removing, or replacing items after the array is created. Dynamic arrays have built-in tools for each of these changes.

Push adds an item at the end

Push puts a new item after the last one. Each push grows the length by one.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract Basket {
    uint256[] public items;

    function addItem(uint256 item) public {
        items.push(item);
    }

    function count() public view returns (uint256) {
        return items.length;
    }
}

Pop removes the last item

Pop takes the last item off the array. The length shrinks by one.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract Stack {
    uint256[] public items = [1, 2, 3];

    function removeLast() public {
        items.pop();
    }

    function count() public view returns (uint256) {
        return items.length;
    }
}

Delete resets one slot to the default

Delete clears one slot back to 0. The slot stays, so the length does not shrink.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract ScoreSlots {
    uint256[] public scores = [10, 20, 30];

    function clearSecond() public {
        delete scores[1];
    }

    function count() public view returns (uint256) {
        return scores.length;
    }
}

Replacing an item by index

Assign to an index to replace that item. The other items stay as they were.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract PriceSlots {
    uint256[] public prices = [10, 20, 30];

    function replaceSecond(uint256 newPrice) public {
        prices[1] = newPrice;
    }

    function count() public view returns (uint256) {
        return prices.length;
    }
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract NumberBox {
    uint256[] public numbers;

    function add(uint256 number) public {
        numbers.push(number);
    }

    function removeLast() public {
        numbers.pop();
    }

    function replaceFirst(uint256 number) public {
        numbers[0] = number;
    }

    function clearSecond() public {
        delete numbers[1];
    }

    function count() public view returns (uint256) {
        return numbers.length;
    }
}

Key points

  • Push: items.push(x) adds x at the end and grows the length by one.
  • Pop: items.pop() removes the last item and shrinks the length by one.
  • Delete: delete items[1] resets that slot to the default value 0.
  • Length after delete: delete leaves the length unchanged, because the slot stays.
  • Replace: items[1] = 99 writes a new value over the item at index 1.

In this example, add pushes 10, 20, and 30 into the empty array. Next replaceFirst writes 99 over index 0, and clearSecond resets index 1 to 0. Then add pushes 40, and removeLast pops it off again. The final array is 99, 0, and 30, with a length of 3.

Note

push and pop work on dynamic arrays, not on fixed-size arrays.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

An array has length 3. What happens to its length when you delete the item at index 1?