Member-only story

Solidity Tutorial: all about Array

Jean Cvllr
10 min readDec 25, 2019

--

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-size
  • T[] : 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-size
  • T[][] : Two-Dimensional, Dynamic-size
  • T[][k] or T[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 (all k are the same)
  • T[2][8][4][12] : Four-Dimensional, Fixed-Sizes ( k‘s are of different values)
  • T[][][][][] : Five-Dimensional, Dynamic-Size
  • T[][3][2][][9][] : Six-Dimensional, Mixed-Size

--

--

Jean Cvllr
Jean Cvllr

Written by Jean Cvllr

Smart Contract engineer at @LUKSO. Full Stack Developer. https://github.com/CJ42

Responses (2)

Write a response