How to create an array in a Solidity smart contract

Phemex Trading

In place of creating different variables of the same data type you can create an array to store a list of elements. Arrays are data structures that store collections of items of the same data type in an assigned location.

The basic syntax to create an array in a smart contract is as follows:

pragma solidity ^0.6.0;

contract samplyArray {
    
    uint[] intergerArray;       //sample array of integers
    bool[] boolArray;           //sample array of booleans
    address[] addressArray;     //sample array of address
    

Try it in Remix

Solidity supports arrays of fixed or dynamic sizes. Arrays have a continuous memory location which means the lowest index is associated with the first element and highest index represents the last element.

In the example below Alice is the first item in the array and is assigned at index location 0 and Cindy is the last item in the array at index location 2.

Value AliceBobCindy
Index 012

The code block below details how to create a state variable array of type dynamic and fixed.

pragma solidity ^0.6.0;

contract samplyArray {
    
    uint[] public myArray; //this is a dynamic array of type uint
    uint[] public myArray2 = [1, 2, 3]; //this is a dynamic array with 1, 2 and 3 as default values
    uint[10] public myFixedSizeArray; //this is a fixed size array of type uint

Arrays have built in functions that allow you to add, update and delete information.

  • Push – adds an element to the end of the array
  • Pop ā€“ removes the last element of the array
  • Length ā€“ gets the length of the array. As an example how many items are in the array
  • delete – allows you to delete an item at a specific location in the array

 //this will add i to the end of myArray
    function pushistoAdd(uint i) public {
        myArray.push(i); 
    }
    

    //returns the value in the specified position of the array
    function getIteminArray(uint index) public view returns (uint) {
        myArray[index]; 
    }

    
    //this will update an item in the array
    function updatethearray(uint locationinarray, uint valuetochangeto) public {
        myArray[locationinarray] = valuetochangeto;
    } 
    
    
    //this is to delete an item stored at a specific index in the array.  
    //Once you delete the item the value in the array is set back to 0 for a uint.
    function remove(uint index) public {
        delete myArray[index];  
    }

Additionally:

  • To declare a dynamic array use []
  • To declare a fixed array use [n] where n= number of items you want in the array
  • The array starts at index 0
  • Elements in a uint array are initialized to their default value of 0.
  • If you try to retrieve an item that is not in the array the log will print an invalid opcode

Below are sample arrays in a Solidity smart contract. The contract contains all of the basic array features you will need in order to get started. You can use this as a reference point to create your own arrays in a smart contract.

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.6.0;

contract samplyArray {
    
    uint[] public myArray; //this is a dynamic array of type uint
    uint[] public myArray2 = [1, 2, 3]; //this is a dynamic array with 1, 2 and 3 as default values
    uint[10] public myFixedSizeArray; //this is a fixed size array of type uint
    
    
    uint[] intergerArray;       //sample showing initialization of an array of integers
    bool[] boolArray;           //sample showing initialization of an array of booleans
    address[] addressArray;     //sample showing initialization of an array of address etc.
    
    
    //this will add i to the end of myArray
    function pushistoAdd(uint i) public {
        myArray.push(i); 
    }
    

    //returns the value in the specified position of the array
    function getIteminArray(uint index) public view returns (uint) {
        myArray[index]; 
    }

    
    //this will update an item in the array
    function updatethearray(uint locationinarray, uint valuetochangeto) public {
        myArray[locationinarray] = valuetochangeto;
    } 
    
    
    //this is to delete an item stored at a specific index in the array.  
    //Once you delete the item the value in the array is set back to 0 for a uint.
    function remove(uint index) public {
        delete myArray[index]; 
    }
    
       
    //this will return the length of myArray
    function getLength() public view returns (uint) {
        return myArray.length; 
    }
       

}

Try it in Remix

If you delete an item in the middle of an array the location in the array is still occupied. So to keep your arrays orderly and compact move the item at the end of your array into the position of the item that you delete.  As an example:

  • Array [1, 2, 3, 4]
  • Lets delete 2
  • We are left with [1, 0, 3, 4]
  • To keep the array compact move the item at the end of the array to the 1 position
  • [1, 4, 3]

function remove(uint index) public {
     //myArray[myArray.length-1]  will return the last element of an array so this complete statement will get the last item in the array and put it in the position that we are removing which is myArray[index].
     myArray[index] = myArray[myArray.length-1] ;  
     
     //then we remove the last element of the array
     myArray.pop(); 

For more information read the docs.

This code is for learning and entertainment purposes only. The code has not been audited and use at your own risk. Remember smart contracts are experimental and could contain bugs.

Click here for more information about how to use the Ethereum test network and how to obtain test ETH.

Next Review – Mapping in Solidity smart contracts

Leave a Reply