Learn how to use arrays in Solidity.

In the previous chapter, we covered the very basics of smart contract development in Solidity. We looked into the core concepts like methods, fields, and variables, and created a simple smart contract.

In this chapter, we'll focus on expanding our Solidity knowledge. We'll learn about data structures in Solidity, such as arrays, and about Solidity’s control structures. We'll also look into how we can define structs, which provide a means to pack multiple fields into a single variable. We'll then use all these new skills in practice and develop several more sophisticated smart contracts.

Dynamic arrays

We'll start this chapter by learning about arrays in Solidity. As in many other programming languages, an array is a data structure that stores a sequence of elements of the same type. Each element has a numerical position starting from zero.

Solidity has two types of arrays:

  • Dynamically sized arrays: Their length can change during runtime, and we can add and remove elements from them.

  • Fixed-size arrays: Their length is determined at compilation time.

We'll first look into dynamic arrays in Solidity. To define a variable of a dynamic array type, we need to specify the type of elements in the array and add the [] characters:

Get hands-on with 1200+ tech skills courses.