A full guide to Arrays in Solidity
In this guide we are going to learn everything about Arrays in Solidity and how to create arrays, add and remove elements from arrays in Solidity.
How to create arrays
To create arrays in Solidity, you have 2 options. You can either create a fixed size array or an array with a dynamic size.
If you're sure that your array will have a specific size, it's best to create a fixed size array so you use less storage and your types are more clearly defined.
Fixed size arrays
To create an array with a fixed size, you have to specify that in the definition of the type of the variable:
address[5] myAddresses;
That way, the myAddresses
array can only have 5 or less items.
And if you do:
address[5] myAddresses = new address[](5)
You will initialise your array with 5 default values.
Dynamic size arrays
To create an array with a dynamic size, all you have to do is... not specify any size!
address[] myAddresses;
That way, myAddresses
has a dynamic size and you can put as many items as you want inside of it.
How to add elements to Arrays, remove elements and update values
Add elements
To add elements, you can use the push
function. It will add a new element at the end of the array:
uint256[] numbers;
function addNumber() public {
numbers.push(5)
}
Remove elements
To remove elements, you can use the pop
function to remove the last element of the array
uint256[] numbers;
function removeLastNumber() public {
numbers.pop()
}
And to remove an element at a specific index, you can use the delete keyword:
uint256[] numbers;
function removeThirdElement() public {
delete numbers[2]
}
Update a value
To update an item, you can use the index of that item and directly change its value:
uint256[] numbers;
function updateThirdElement() public {
numbers[2] = 5
}
Get the length of an array
To get the length of an array you can use the length
property:
uint256[] numbers;
function getLength() public view returns (uint256[]) {
return numbers.length;
}
Differences between memory and storage arrays
The last thing to have in mind is that you cannot use the push
and pop
functions on arrays that are created with the memory
keyword (or calldata
).
You can only use them on arrays that use the storage
keyword (explicitly or not). If you define a property at the top of your smart contract, it uses the storage
by default so the array methods are available.
uint256[] myNumbers;
// does not work
function addElementToArray(uint256[] memory array) public {
array.push(5) // won't work
}
// does work
function addElementToArray(uint256[] storage array) public {
array.push(5) // will work
}
// does work
function addToNumbers() public {
myNumbers.push(5) // will work
}
And that's it 🎉
Thank you for reading this article, if you want to go further and get better at blockchain development, leave your email below and you'll get:
- access to the private Discord of a community of Web3 builders
- access to free guides that will teach you a subject from scratch like the Web3 JS Cheat Sheet