Like many programming languages, Solidity uses both dynamically sized arrays and fixed-sized arrays. First, let's see how to declare a dynamic array of integers. First we write the data type. In this case, we create an array of numbers. And the data type we will use is uint256.
Then we make it public and call it dynamicArray. Okay. Now we declare a fixed-size array of integers and specify the size as 10. Then we write uint256. Then we specify its size in square brackets.
Let's make it public and call it fixed Array. One thing to note is that both arrays are declared as state variables, which means they are stored in contract storage. This is important because Solidity has two types of arrays: storage arrays, like the ones just declared, and in-memory arrays. We'll talk more about in-memory arrays later, but for now it's important to remember that storage arrays are stored in storage. We will learn more about the storage layout for a storage array.
But for now let's focus on dynamic arrays. Suppose we want to add elements to the dynamic array. How can we do it? We create a function called addElementToDynamicArray that takes a uint which we will call newElement and this function will be external. Okay.
So we simply write dynamicArray.push and add the new element. What we're doing is adding this new element that we're passing as an argument to this dynamic array, using the push function. Ok, let's try it passing as an argument to this dynamic array, using the push function. Ok, let's try it .
We distribute our contract. And now we simply call addElement and add the value, let's say nine. Ok, the transaction was successful. You may have noticed that we have two functions here that are actually automatically generated by Solidity, and that's because we're using the public keyword. So we can call the dynamicArray and fixedArray functions, but they are getters.
As you can see, they accept a uint256 representing the index. If you remember, arrays start at index zero. So if we call dynamicArray with a value of zero, we should get the value inserted at position zero. If we call with index one, you will notice that the transaction is canceled because there is nothing there. Okay.
So let's add a new element. Let's say 12. The transaction was successful. And now we call the dynamic array with index one. And we should get 12 because the second element added to the dynamic array is added to the next position, which is one.
And as you can see we get 12. Now we extend the contract and include a function called remove last element of dynamic array. And this one will be external too. And what we're going to do is just dynamic array pop. Then the pop function will delete the last element of the array.
In this case, the first time we call this function, it will delete the element stored at index one. In this case the element with value 12 and if we do it again it will delete the element of index zero which is nine. Okay. So let's try it. Let's deploy and then add item 9 and item 12 again.
Okay, let's verify that the values are there. We get nine and we get 12 there. We get nine and we get 12 . Okay. Now if I call remove last item, it succeeds.
And now let's try to get index one. And as you can see, we're getting an error because it's been deleted. So let's try with the zero index. It's a success and we get number nine. So let's remove it now.
Remove. It went well. If we try to get the value at index zero, the operation will be rolled back. One thing to keep in mind is that, unlike other languages where the pop function returns the value that was in the last element of the array when it was removed, this is not the case in Solidity . For example, if we wanted to get that value, say last deleted element equals dynamic array.
pop, we would get an error. Why? Because dynamic array, as it says, the right side returns zero. So it doesn't give anything back. This is a small difference with Solidity.
Automatic transcript — names and jargon may be misspelled.