Solidity Arrays (Fixed-Sized and Dynamic-Sized)

whiteboard crypto logo
Published by:
Whiteboard Crypto
on

Arrays are a data structure that organize information in a list. In the language of Solidity, arrays all store the same type of information, and can be thought of as an X-Y table, where the X column is an auto-incrementing number, and the Y column is the user’s input. Here’s are a few arrays:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;


contract arrays{

    uint[] public fingers;
    uint[2] public toes = [10,10];
    address[] public whiteList;

    function getArray() public view returns (uint[2] memory){
        return toes;
    }

}

The first example is creating a dynamic array named “fingers” which is not initialized. In the case of arrays, dynamic means the size of the array can change throughout time, and since it is not initialized, there is no data in it.

Secondly, we have created a fixed-sized array named “toes”, which can only be 2 items long, as indicated by the 2 inside the brackets. We have also initialized these two items with the value of 10 each.

In both of these examples, we have used the data type unsigned integers to create the array, however we can create arrays with other variables as well.

The third example is a dynamically-sized array named “whiteList”, which is not initialized. This is useful if you want to store a list of addresses on the blockchain, however it is not as useful as using a mapping if you’re actually wanting to use a whitelist function.

In a fixed-sized array, the default value of an item if it is not initialized is always “0”.

If you wish to view an entire array, you can create a new function to return it. When viewing an array using Remix, it will only show you each item as you request it, so creating a “getter” function will come in handy. You can see in our example above that our “getArray” function returns a uint array that holds 2 items, and it specifically returns the array we previously created named “toes”.

Add an item to an array

In the case you want to add something to a dynamic array in Solidity, we can utilize the array member named “push”.

fingers.push(9);
//This will add "9" to the end of the array
//This will also increase the length of the array "fingers"

Remove an item to an array

In the case you wish to remove the last part of a dynamic array in Solidity, we can utilize the array member named “pop”. It should be noted that this will only remove the last item, and can not be used to remove a specific item.

fingers.pop;
//This will remove the last item in the array
//This will also reduce the length of the array "fingers"
//You can only remove the last item

Delete an item from an array

If you want to remove an item in the middle of an array, you can’t. Instead, you can reset it’s value to “0” by calling the delete keyword and indicating the specific item you wish to remove. It’s important to know that this doesn’t remove that item, it only resets it, even the length will stay the same.

remove fingers[1];
//fingers[1] will now be equal to "0"
//fingers.length stays the same

How to get the length of an array

It is very easy to get a value of the length of an array in Solidity, the solution is to use the array member “length”. This is useful if you want to loop through all the items in an array, you will need the total length to know how to initialize the loop.

fingers.length;
//this will return the total length of the array named "fingers"

How to remove a specific item from an array

If you want to remove a specific item from an array in Solidity, you can create a function that does it very carefully using a for loop to iterate through each item, reassign it’s value to the item after it, and then delete the last item. Here’s an example:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;


contract arrays{

    uint[] public toTen = [0,1,2,3,4,5,6,7,8,9,10];

    function remove(uint _index) public {

        for (uint i = _index; i < toTen.length - 1; i++) {
            toTen[i] = toTen[i + 1];
        }

        toTen.pop();
    }

    function displayArray() public view returns(uint[] memory){
        return toTen;
    }

}

This contract creates a new unsigned integer array named “toTen” and initializes it with the numbers 0 through 10. Then, the function named “remove” accepts a number. The number you give it will be the item in the array it will remove by using a loop and the pop function. You can view if this was successful by using the “displayArray” function, which displays the entire array before and after utilizing the remove function.

whiteboard crypto logo

WhiteboardCrypto is the #1 online resource for crypto education that explains topics of the cryptocurrency world using analogies, stories, and examples so that anyone can easily understand them. Growing to over 870,000 Youtube subscribers, the content has been shared around the world, played in public conferences and universities, and even in Congress.