Member-only story
Solidity Tutorial: all about Array

In Solidity, an array
is an ordered list of items that is indexed numerically, starting at 0.
Array types
In this section, we provide two types of categorisation for arrays:
- Fixed-size vs dynamic size arrays
- One-dimensional vs Multi-Dimensional arrays.
Fixed-size vs Dynamic-Size array
The Solidity documentation make the distinction between fixed-size and dynamic size arrays. In the examples below, T
is the element type and k
is the array length / size. If k = 5
, the array can hold a maximum of 5 values.
- Fixed Size:
T[k]
// array that can hold 5 unsigned integers maximumuint[5] my_array;
- Dynamic Size:
T[]
// array containing an undefined number of stringsstring[] my_array;
One Dimensional vs Multi-Dimensional arrays
One Dimensional and Multi-Dimensional arrays can be both of Fixed-Size or Dynamic-Size. The One Dimensional arrays are essentially the examples cited above. Here are the definitions
T[k]
: One Dimensional, Fixed-sizeT[]
: One Dimensional, Dynamic-size
Multi-dimensional arrays are essentially nested arrays (An array that contain other arrays). These however, come in three forms in Solidity. We use two-dimensional array in this first example…
T[k][k]
: Two-Dimensional, Fixed-sizeT[][]
: Two-Dimensional, Dynamic-sizeT[][k]
orT[k][]
: Two-Dimensional, Mixed-size
… but you will see that multi-dimensional arrays can have any level of nesting ! Here are some examples.
T[2][2][2]
: Three-Dimensional, Fixed-Size (allk
are the same)T[2][8][4][12]
: Four-Dimensional, Fixed-Sizes (k
‘s are of different values)T[][][][][]
: Five-Dimensional, Dynamic-SizeT[][3][2][][9][]
: Six-Dimensional, Mixed-Size