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.

TypesOverview
Static ArraysStatic 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 ArraysDynamic arrays have a variable length that can be changed at runtime.

Declare Array

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 :

uint numbers[10];

Programmers declare dynamic arrays in Solidity by just specifying the element types as follows:

type[] arrayName;

Initialize Arrays

It is possible to initialize the Solidity array elements one at a time or with a single statement as shown below:

uint number[10] = [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[4] = 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[3];

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.


Members

MethodsOverview
LengthThe length of an array is calculated by this method. A dynamic array’s length can be changed by setting it.
PushAdds an element to a dynamic array. It provides the modified length of the array.

The following example shows the working of Solidity arrays:

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0;contract SolidityTest{ uint loop_element; uint length_array;string[] software_houses_in_USA=["Microsoft","Capital One","Cisco Systems","Northrop","Verizon"];function display_Array() public returns (string[] memory){ for(loop_element=0;loop_element<software_houses_in_USA.length;loop_element++){software_houses_in_USA[loop_element]; length_array=software_houses_in_USA.length;} return software_houses_in_USA;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

You will see below output on execution of above examples:

Solidity Arrays examples

You can have a better understanding regarding the static array by going through the example below:

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0;contract SolidityTest{uint mrx; uint[] Even_Numbers;function get_Array_Elements() public returns(uint[] memory){for(mrx=0;mrx<=10;mrx++){uint value=mrx*2; Even_Numbers.push(value); } return Even_Numbers; }uint[] prime_Numbers=[2,3,5,7,11,13,17];function Static_Array() public returns(uint){uint value=prime_Numbers[2];return value; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>
The Dynamic array can be initialized in the following way:

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0;contract SolidityTest{function table_of_5() public pure returns (uint[] memory){uint[] memory myArray=new uint[](12);myArray[0]=5; myArray[1]=10; myArray[2]=15; myArray[3]=20; myArray[4]=25; myArray[5]=30; myArray[6]=35; myArray[7]=40; myArray[8]=45; myArray[9]=50; myArray[10]=55; myArray[11]=60;return myArray; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Conclusion

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.

Note: When working with Solidity arrays, it’s important to consider the gas costs of array operations, especially for large 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.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *