Dynamic Arrays
The Cooler Version of Static Arrays
Recall in Data Structures that we can declare a static array as follows:
where T
is the type of the array. The good news is that defining a dynamic array is not that much different; below is the syntax for declaring a dynamic array:
Declaring a dynamic array seems simple, but what about defining a dynamic array? Below is the syntax to define a dynamic array:
Since dynamic arrays can vary in size, we would ideally like to have functions available to us which allow us to grow/shrink our array. And indeed, Solidity provides us with such functions.
array.push(x)
: pushesx
to the back ofarray
array.pop()
: pops the last element ofarray
And like with static arrays, we can also index into dynamic arrays; it should be noted, however, that this can cause runtime errors if the user is attempting to index into an undefined location of a dynamic array.
Last updated