Understanding Arrays in Solidity.
To be able to master Solidity, you MUST know what Arrays and Mappings are. These are basic concepts that help you to understand how to properly write Solidity. Arrays in Solidity are what you use to store a list of sorted information. You can access this piece of information whenever you need them. You can store a list of names, [Adam, Bam, Joe], or a list of numbers [1,4,5]. Let's create an Array of String containing names of persons.
In the Array above we use the string[] to declare that this array contains elements of string type.
Next, we name the Array as stringNames, and then declare it as public.
We then assign the string values [Adam, Mustapha, Becky] to the string.
Let us create an Array of Integers.
First, we declare the array and the data type that will be declared in the Array uint[]. This means that we will not be able to store any other data in this array. Then we declare the Array Public with the public keyword. We then name our Array as unsignedIntegers. After this, we assign the values of the integers to the Array.
You can create a 2 dimensional Array in Solidity. This means an Array inside an Array. You can create it to store complex data sets. You can do this by creating it this way as shown below.
The 2 dimensional Array can be used to create complex data sets. From the code above. We declare that the Array is a two dimensional Array and the datatype with unit[][]. We then name the Array as 2DArray and make it public with the Public keyword. The Value of the individual Arrays in the 2Dimensional Arrays is assigned to the Array. Now let us see how we can use an Array inside a function.
This function adds another value to the values Array with the .push function. When this function is called, we can pass in any number and that number will be added to the Array.
To be able to determine the length of an Array, we do it this way.
This function named valuelength calls the .length on the Array of Values. This then returns the number of items in the Arrays.
In the next article, we will look at what Mappings is in Solidity. You can follow me on Twitter to follow my Journey of learning solidity and becoming a Blockchain developer.