You can declare an array in Solidity.
type[length] name = [ ... ];
Arrays in Solidity are fixed collections of elements with the same data type values. Each element has an index(position) within the array. An array is useful for holding fixed or unknown collections of values without having to create a different variable for each value.
Let's say you have vote counts from five polling units. Instead of creating five variables to hold each of these votes count, you create a single array to hold them.
uint votes = [10, 20, 8, 88, 14];
An array starts with the type, length, identifier, and values.
Type is the data type of the array. In the above example, we specify this array should contain only unsigned integers. Solidity support multiple types e.g string.
Length of the array specifies the total values that the array can hold. Our example we set the array to hold only five values. If your array may contain unknown or infinite values, then you can just leave the length empty
Identifier is the name of the array. Just give it any name that makes sense.
Values are the values in the array.
Hope that helps!
Recommended: How to get the length of an array in Solidity
Top comments (0)