Quick Guide To Solidity Arrays
In this article, we will explore Solidity arrays in depth, looking at their types, syntax, and functionality, as well as some best examples for working with arrays in Solidity.
Arrays are data structures that store sequential collections of similar elements of a fixed size.
Arrays contain a collection of data, but you can think of them as collections of similar variables.
It is preferable to declare one array variable like numbers instead of individual variables like number0, number1, …, and number99. A specific element in an array is accessed by an index.
Solidity arrays can have a compile-time fixed size or a dynamic size. Similarly, storage arrays can also have different types of elements. Memory array element types cannot be mappings, and if they are to be used as function parameters, then they should be ABI types.
An array is made up of interconnected memory locations. It is the lowest address that points to the first element, while the highest address refers to the last element.
Solidity Array Types
Solidity supports both static and dynamic arrays.
|Static Arrays||Static arrays have a fixed length that is defined at compile-time. Once the length of a static array is defined, it cannot be changed.|
|Dynamic Arrays||Dynamic arrays have a variable length that can be changed at runtime.|
To declare an array of fixed size in Solidity, the programmer specifies the type of the elements and the number of elements required by an array as follows:
Arrays of this type are called single-dimension arrays.
To declare an array make sure that the array size must be set to a positive integer constant greater than zero, and its type must be any Solidity data type.
Here is an example of how to declare a 10-element array known as balance of type uint :
Programmers declare dynamic arrays in Solidity by just specifying the element types as follows:
It is possible to initialize the Solidity array elements one at a time or with a single statement as shown below:
uint number = [1, 2, 3,4,5,6,7,8,9,10];
It is not possible to declare a greater number of elements for the array between square brackets [ ] than the number of values between braces [ ].
When the array size is omitted, an array that is big enough to hold the initialization is created.
The following example shows how to assign one element to the array.
uint number = 5
Here we have accessed the 5th element of the given array.
Creating Dynamic Memory Arrays
The new keyword creates dynamic memory arrays.
uint mrx = 4; uint numbers = new uint(mrx);
Accessing Array Elements
A member of an array is accessed by indexing its name. In this case, the index of each element will be enclosed within square brackets after the array name. For example:
uint value = number;
In the above statement, the value of the fourth element will be assigned to the variable named value.
Below example will incorporates all three of the above-mentioned concepts, declaration, assignment and accessing arrays.
|Length||The length of an array is calculated by this method. A dynamic array’s length can be changed by setting it.|
|Push||Adds an element to a dynamic array. It provides the modified length of the array.|
The following example shows the working of Solidity arrays:
You will see below output on execution of above examples:
You can have a better understanding regarding the static array by going through the example below:
Solidity arrays are important data structure that allow you to store and manipulate collections of values.
Solidity supports various types of arrays, including dynamic arrays | fixed-size arrays and multidimensional arrays.
To minimize gas costs, you can use fixed-size arrays instead of dynamic arrays if the size of the array is known in advance.
Additionally, you can use the memory keyword to create temporary arrays that are only stored in memory during function execution.