March 1, 2022

Mastering Arrays in Solidity

Kristaps Grinbergs

Arrays in Solidity programming language can hold primitive data types like integers, boolean, and string. An array can be initialized with a fixed or dynamic size. If we compare them to other more advanced programming languages, there are pretty limited functions available to work with arrays. Despite that, we can add, remove, get the size of the array, and more.

Initialize an array

We can create an array in Solidity, either having a dynamic or fixed size of the elements that it can hold.

Using a fixed-size array can reduce gas costs but not always we can predict the size of the elements.

Dynamic size

When creating a dynamic array in Solidity, we need to provide the data type that it can hold. Unfortunately, arrays can't have multiple data types in one array.

Fixed size

Another option is to provide the size of the elements that an array can hold. This can save gas costs, especially when creating smart contracts on the Ethereum blockchain.

Behind the scenes, Solidity fills the array with default values, which for the uint256 is 0 (zero).

We can also provide the values when initializing an array.

Add an element

We can add a new element for the dynamic type of arrays. To do that, we need to use the push method. It adds an element to the end of an array.

Here is an example of adding 100 to the array that holds uint256 values.

Change an element

When changing an element, we need to provide the index and the new value.

Let's say we want to change the first element's value in the array.

Get the size of the array

To get the size of the array then, we should use the length function.

Remove

We can remove an element from array by using the delete function. The catch here is that it doesn't change the size of the array. It resets to the default value, which for instance, in the uint256 case is 0 (zero).

This example resets the first element in the array to 0 (zero).

If we want to remove an element and change the array's length, we can move all elements up from the element we want to delete. Then pop the last element because it isn't needed anymore.

TL;DR

Arrays in Solidity programming language can hold primitive data types like uint, boolean, address, or string. We can create a fixed or dynamic size array. A fixed-size array has less implications for gas costs. We can change elements, remove, get the size of the array.

Links

Sample codeOfficial documentationSolidity by Example - Array