Quick and faster way to understanding arrays in solidity

Photo by Max Duzij on Unsplash

Quick and faster way to understanding arrays in solidity

Understand arrays once and for all.

Arrays in Solidity

Different people are always scared when they heard of array in solidity or in programming at large. This article is going to be focusing on arrays in solidity.

Let start by defining what an array is.

Array is a data structure, which saves collections of elements of the same type. An array is used to save or store collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Types of array

  1. Fixed array

  2. Dynamic array

Let start with Fixed array

  1. Fixed array is an array whose size is determined during the declaration. For example:
uint[3] public nums = [1,2,3];

Let explain the above code. variable type was declared which is unsigned integer(uint). This is used for numbers without decimal. After which we have visibility type which is public, followed by the name of the array. Then we added 1,2 and 3 to the array.

2 Dynamic Array is an array whose size is not determined during the declaration in other words, the size of this array is unlimited(infinite).

uint[] public nums = [1,2,3];

As you can see here, the variable type was defined as uint followed by the array square bracket, after which we have the visibility type(public) then array name which is nums. We notice here that the array size was not specified and that makes the array to be unlimited so to say.