What are Solidity Arrays and how to use it – Complete Guide

What are Solidity Arrays and how to use it – Complete Guide

Hello Developers ?,

Hope you all are doing well, in this article we will discuss the arrays in solidity. You might have learned the concept of arrays in a data structure or in any other programming language, but arrays in solidity are a little different from that, let’s see how arrays work in solidity and how can you implement them in an efficient way.

What are solidity arrays?

An array represents a sequential collection of elements of the same data types stored in memory, in layman language array stores the values of the same data type in sequential order in memory, for example, a list of numbers, or a list of names.
In solidity, arrays can be of fixed or dynamic size.

1 . Fixed size array: the size of an array is fixed (predefined) at the time of compilation

data_type[size] array_name = <elements>;Code language: JavaScript (javascript)

2 . Dynamic size array: dynamic means the size of an array can change

data_type[] array_name = <elements>;Code language: JavaScript (javascript)

How to initialize an array?

To initialize an array first we need to mention the data type of elements present in an array followed by the name of an array, and [ ], if the array is of fixed size we need to mention the size of an array wrapped inside [ ] otherwise if an array is dynamic just keep [ ] empty. The data type should be a valid solidity data type.

The syntax for initializing an array

data_type[size] array_name; // fixed size array
data_type[] array_name; // dynamic size arrayCode language: JavaScript (javascript)

for example,

uint[4] nums = [1,2,3,4];
uint[] marks = [];Code language: JavaScript (javascript)

you can make the array public to create a getter function for an array

uint[4] public nums = [1,2,3,4];Code language: JavaScript (javascript)

How to access the elements of an array

To get an element of an array we need to specify the index number of that element

In the above example, we created a fixed-size array of four elements.
let’s say we want to access the element at position number 3, so we created a new variable ‘x’ of data type uint as the array is of uint type, to store the fetched value. first, we need to write the name of an array and then mention the index number inside the [ ], of an element we wanted to access.

How to insert a new element in an array

In a fixed-size array, we can not insert a new element as it increases the length of an array.
To insert a new element in the dynamic size array we use the push method.

Using the push method we can insert a new element in an array, push method inserts a new element at end of an array which is the last index of an array, in sequential order.

How to remove the last element of an array

Just like we can not insert a new element in a fixed-size array as it changes the size of an array we also can not remove an element from fixed-size arrays.
To remove the last element of the dynamic size array we use the pop method.

Using the pop method we can remove the last element of an array, which decreases the length of an array by 1.

How to delete an element of an array

The delete method deletes the element of an array and resets it to its default value, as it does not change the length of an array we can use the delete method on dynamic as well as a fixed-size array.

How to get a length of an array

Solidity provides a special property for an array to find its length.

Resources

Where can you learn more about solidity arrays?

The best way to learn solidity concepts is to follow the official documentation of solidity. The official documentation is always are up to date resource to check out any concept.

Besides official documentation, you can also check out the Master Solidity course by Codedamn in which arrays are covered in depth.

Also, you can use Codedamn’s Solidity Playground to practice solidity code which is free to use.

Conclusion

Arrays in solidity are easy to understand but complex to implement as storage arrays get stored on to the blockchain and to perform certain operations like updating, and inserting on storage arrays requires gas cost which might be a nightmare if arrays are not implemented correctly, so it is must implement arrays correctly so that it will execute properly on a chain and will give optimized output.

Also, there can be different ways to perform a single task but it is important to realize which one will cost less gas and will give optimized output, so it is our responsibility as a developer to implement the correct way.

Hope you all enjoyed reading the article and understood the concept of arrays in solidity.

Sharing is caring

Did you like what Shashank Wangkar wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far

Curious about this topic? Continue your journey with these coding courses: