Arrays

Learn about arrays in detail.

Arrays are data structures that have a fixed set of identically typed items with a unique index for each. Instead of creating many distinct variables of the same type, we simply declare one array of the required size, place the items in the array, and then use the index to access the array’s members. The lowest index in an array represents the first element, while the highest index represents the last element.

Press + to interact
Array values mapped by indexes
Array values mapped by indexes

In this Solidity array example, each index is inclusive of a specific string element. Index 0 holds Educative, index 1 holds Solidity, and index 2 holds Course. This array structure allows for easy access to each of these string elements.

An array’s size in Solidity can be either fixed or dynamic.

To declare an array in Solidity, we need to specify both the data type of its elements and the number of those elements. For fixed-size (nondynamic) arrays, the size must be a positive integer, and the data type has to be one that Solidity recognizes. The syntax is as follows:

<data type> <array name>[size] = <initialization>
The syntax for array declaration in Solidity

Fixed-length arrays

In this type of array, the size must be predetermined. The array can’t hold more elements than its declared size. If we don’t specify the size, an adequately large array will be created ...