3 min read

Arrays

Arrays in Solidity are a powerful data structure that allow you to store and manipulate a collection of elements of the same type. You can use arrays to store everything from simple integers to complex structures.

In this blog post, we'll cover the basics of declaring and using arrays in Solidity 0.8.16.

Declaring Arrays

In Solidity, arrays can be declared with the following syntax:

// Static array with a fixed size of 5 elements
uint[5] myArray;

// Dynamic array with no fixed size
uint[] myDynamicArray;

// Array of a custom type
struct Person {
  string name;
  uint age;
}

Person[] people;

In the above example, we've declared three different arrays. The first is a static array with a fixed size of 5 elements. The second is a dynamic array with no fixed size. And the third is an array of custom Person structs.

Accessing and Modifying Array Elements

Once you've declared an array, you can access and modify its elements using the following syntax:

uint[5] myArray;

// Accessing an element
uint firstElement = myArray[0];

// Modifying an element
myArray[1] = 10;

In the above example, we've declared a static array with a fixed size of 5 elements. We then access the first element of the array and assign it to a variable called firstElement. Finally, we modify the second element of the array and set it to the value 10.

Iterating Over Arrays

You can iterate over an array using a for loop. The following example demonstrates how to loop over an array of integers and print out each element:

uint[] myArray = [1, 2, 3, 4, 5];

for (uint i = 0; i < myArray.length; i++) {
  uint element = myArray[i];
  // Do something with element
}

In the above example, we've declared a dynamic array of integers with the values [1, 2, 3, 4, 5]. We then use a for loop to iterate over the array, accessing each element in turn and assigning it to a variable called element.

Array Length

You can get the length of an array using the length property. The following example demonstrates how to get the length of a dynamic array of integers:

uint[] myDynamicArray;

uint arrayLength = myDynamicArray.length;

In the above example, we've declared a dynamic array of integers called myDynamicArray. We then get the length of the array and assign it to a variable called arrayLength.

Array Functions

Solidity provides several built-in functions that you can use to manipulate arrays. Here are a few examples:

uint[] myArray;

// Push a new element onto the end of the array
myArray.push(10);

// Remove the last element from the array
myArray.pop();

In the above example, we've declared a dynamic array of integers called myArray. We then use the push() function to add a new element to the end of the array, and the pop() function to remove the last element from the array.

Conclusion

In this blog post, we've covered the basics of declaring and using arrays in Solidity 0.8.16. Arrays are a powerful data structure that can be used to store and manipulate collections of elements of the same type. We've covered how to declare arrays, access and modify their elements, iterate over them, get their length, and use built-in functions to manipulate them.

Arrays are an important tool in Solidity, but it's important to keep in mind their gas costs. Arrays can be expensive to use, especially if they are large or used in tight loops. As a result, it's often a good idea to consider using other data structures or optimizing your code to avoid unnecessary array operations.

Overall, understanding how to use arrays effectively is an important skill for any Solidity developer. With the knowledge you've gained in this blog post, you should be well-equipped to start using arrays in your own Solidity smart contracts.

Feedback

Have feedback? Found something that needs to be updated, accurate, or explained?

Join our discord and share what can be improved.

Any and all feedback is welcomed.