Understanding Array in Solidity (Push, Pop, Length)
Tags: webdev, web3, smartcontract, solidity
Published: 2022-11-22 08:23:45

**Table of Contents**

- Arrays
- Array Types
- Array Members
- Removing Array element by shifting to the last and using pop() command.

## **1) What is an Array?**

Array is a data structure used to store a collection of data or variables of the same type. Instead of declaring *number = number1, number2, number3. . . number(nth)*, we can group it as *number[] = [1, 2, 3. . . nth].*

What we simply did here is instead of declaring numberous individual value repeatedly, we create one array and allocate all the value in one collection of array where each value can be accessed by the index.
Now let’s talk about index. The first value in any array is of index *0*. Ie : for *number[] = [1, 2, 3. . . nth]* discussed earlier, the index *0* denoted as *number[0] = 1, number[1] = 2, number[2] = 3. . . number[n] = n+1.
*
## Creating an Array

To create an array in Solidity, the data type of the elements and the number of elements should be specified. The size of the array must be a positive integer for fixed-sized arrays and data should be a valid Solidity type.

**Syntax:** *type[] public arrayName = [elements]*

*type[]* is the data type of the values in the array, where *[]* shows it’s an array.
*arrayName* is the name of the array defined while *elements* is the value or elements of the array. The elements should be the same value type.

**Code example**

*In the code example below, the contract myPractise defines a contract, an array type uint named arrayValue is created, values are assigned to the array defined.*


```
pragma solidity ^ 0.8.0;
// //SPDX-License-Identifier: MIT;
 
contract myPractise {
 
uint[] public arrayValue = [1, 2, 3, 4, 5, 6, 7];
}
```

## Accessing an Array

An element in an array is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example  given an array 

 
 uint[] public arrayValue = [1, 2, 3, 4, 5, 6, 7];
 

We can access any value of the array. For instance, to access the value 4 in the array, we use the index 3, that is *arrayValue[3]* which outputs *4* if we run it.

**Code Sample** :

In the code example below, the contract *myPractise* defines a contract, an array type uint named *arrayValue* is created, values are assigned to the array defined. The function *getArrayValue()* is created to get the index value of 3. We then assigns *x* to the index then returns *x* to get the index value.


```
pragma solidity ^ 0.8.0;
 // //SPDX-License-Identifier: MIT;
 
 contract myPractise {
 
 uint[] public arrayValue = [1, 2, 3, 4, 5, 6, 7];
 function getArrayValue() public view returns (uint) {  
 
 uint x = arrayValue[3];
 return x;}
 }

 //Ouput = 4

```
 
Output : 4.




## 2) Array Types

### Dynamic Array

Dynamic arrays are not predefined and are modifiable. The size and length increases automatically when we add an element to the array.

Syntax : *type[] public arrayName = [array1, array2, array3. . .]*
 

**Code example** :

 ```
uint[] public dynamicArray = [1,3,5,7,9];
```


### Fixed-Sized Array

As the name suggests, the size of the array are predefined and fixed. This type of array are not modifiable. Note also that the number of array elements can not exceed the predefined size.

**Syntax** : *type[n] public arrayName = [array1, array2, array3, array(nth)]*

In the above syntax, n is the predefined size value.


**Code sample** :

```
uint[5] public fixedArray = [0,2,4,6,8];
```


## 3) Array Members

### Push()

Push is used to add new element to a dynamic array, when you push a value to an array, it becomes the last value (nth element)

**Syntax** : *arrayName.push(x)*

**Code example** : 

In the code example below, an array type uint named arrayValue is created, values are assigned to the array defined. The function popArrayValue() is created to add value to at the end. With the synthax arrayValue.push(8) we add 8 to the array, which becomes the last value of the array.


```
uint[] public arrayValue = [1, 2, 3, 4, 5, 6, 7];
function pushArrayValue() public {  
arrayValue.push(8);}
```


### Pop()

Pop is used to delete or remove an element in a dynamic array from the end.

**Syntax** : *arrayName.pop()*

**Code example** :

In the code example below, an array type uint named arrayValue is created, values are assigned to the array defined. The function popArrayValue() is created to remove the last value. With the syntax arrayValue.pop() we remove the last value, 7.

```
uint[] public arrayValue = [1, 2, 3, 4, 5, 6, 7];
 function popArrayValue() public {  
 arrayValue.pop();}
```


### .Length

Length of an array is the number of the elements contained in an array. The length of an array is of value type, uint. Length of an array is fixed when declared but can change depending on runtime parameters or modification.

**Syntax** : *arrayName.length*

**Code sample** : 

In the code example below, an array type uint named arrayValue is created, values are assigned to the array defined. The function *lenthOfArrayValue()* is created to get the length of the array. We assigns *x* to *arrayValue.length* to return the length of the array.

```
uint[] public arrayValue = [1, 2, 3, 4, 5, 6, 7];
 function lengthOfArrayValue() public view returns (uint) {  
 uint x = arrayValue.length;
 return x;}
```


### Delete

Delete is used to remove the index value in an array and automatically set it to 0.

**Syntax** : *delete arrayName[x]*

**Code sample** : 

In the code example below, an array type uint named arrayValue is created, values are assigned to the array defined. The function deleteArrayValue() is created to delete a value from the array. We use delete arrrayValue[2[ to delete the index value 3 from the array, which sets it to 0.

*#Note that 2 is the index number of the index value 3.*

```
uint[] public arrayValue = [1, 2, 3, 4, 5, 6, 7];
function deleteArrayValue() public {  
delete arrayValue[2];}
```

### The difference between delete and pop()

**Delete** removes an array value in any location, while **pop()** only removes a value at the end of an array.

**Delete** removes a value and automatically sets it to 0, while **pop()** entirely removes a value from an array.

**Delete** is executed with the command ‘*delete arrayValue[x]*’ while **pop()** is executed with the command ‘arrayValue.pop()’.

## 4) Removing Array By Shifting To The Last And Using The Pop()

**Code sample** : 

In the code example below, an array type uint named arrayValue is defined, values are assigned to the array defined. The function removeValue(_input) is created to accept input. We then shift the input value to the last value and pop() the last value.

 
 
```
//REMOVING THE VALUE OF ARRAY BY SHIFTING THE VALUE INPUTED TO THE LAST,
 //THEN POP THE LAST VALUE
 
 uint[] public arrayValue = [1, 2, 3, 4, 5, 6, 7];
 function removeValue(uint _input) public  {
    //This shifts the input value from the array to the last value in the array
    arrayValue[_input] = arrayValue[arrayValue.length - 1];
    arrayValue.pop();}
```


## Array Literal

An Array literals is a comma-seperated list of one or more elements in a square bracket. The elements in the array of a literal array should be of the same type otherwise it throws an error because the elements can not be implicitly converted.

For example, the array literal [1, -1] is invalid because the type of the first element is uint8 while the type of the second is int8 and they can not be implicitly converted to each other to make it work, e have to use [int(8), -1]

Code example : 



### CONCLUSION

**That's a wrap guys!**
 
I've tried to cover the basics of Arrays in solidity. It's up to you to do more research on this topic for deep knowledge. Expect more indepth research and resources on Web3, Smart Contracts, and Blockchain in my subsequent Blog posts.
 
I'm Sunington, you can connect with me on [Twitter](https://www.twitter.com/sunington) and on [LinkedIn](https://www.linkedin.com/in/sunnyakujobi).
 
Until we meet again

**#KeepSunning**