Arrays are ordered data structures that store multiple elements of the same type. Each element is positioned in a sequence, and the array’s index allows for the direct retrieval of any element. In the context of smart contract development, arrays provide a way to organize and manage data like addresses, token balances, and metadata.
An array consists of three components:
n-1
, given there are n
elements. This allows for quick access to any element using its index.
int myArray[5]; // Creates an array of 5 integers
In the array declaration int myArray[5];
:
int
(integer): Is the element type and specifies all elements in the array are integers (whole numbers like 1, -3, or 100). When all elements share the same data type, processing is more consistent.myArray
: The identifier (or name) of the array.[5]
: Defines the fixed size and the indexing of the array. This array holds exactly 5 elements and the index ranges from 0 to 4. some text// initializing values in braces
int myArray[5] = {0, 1, 2, 3, 4};
string[] strArray; // Declares a dynamic array of strings
In the array declaration string[] strArray;
:
string
: Is the element type that specifies all elements in the array are strings (sequences of characters like "hello," "blockchain," or "contract"). strArray
: The identifier (or name) of the dynamic array.[]
: The size of the array is not fixed. Elements can be added or removed, allowing the array to dynamically grow or shrink during runtime.
Arrays (or similar structures) are supported in a wide range of programming languages. These languages may offer variations like dynamic arrays, fixed-size arrays, or vectors, providing flexibility for different data handling needs.
In web3 development, arrays enable efficient data storage and retrieval. They help manage lists of addresses, balances, and transaction logs. Here are four examples of how arrays are declared and used in popular smart contract languages: Solidity, Vyper, Rust, and Move.
1. Solidity (Arrays)
// Declare a dynamic array of unsigned integers
uint256[] public values;
2. Vyper (Lists)
# Declare a fixed-size list of integers
values: public(int128[10])
3. Rust (Arrays)
// Declare a fixed-size array of 5 unsigned 32-bit integers
let values: [u32; 5] = [1, 2, 3, 4, 5];
4. Move (Vectors)
// Declare a vector (Move's equivalent of an array) of unsigned integers
let values: vector = vector[1, 2, 3];
Each language uses different syntax for declaring arrays, but they all follow the core principle of storing elements in an indexed sequence.
Arrays can store various data types, depending on the requirements of a decentralized application or smart contract. Here are a few examples:
Addresses
address[] public users;
users: public(address[5])
let users: [String; 3] = ["0x123", "0x456", "0x789"];
let users: vector<address> = vector[0x123, 0x456, 0x789];
Token balances
uint256[] public balances = [100, 200, 300];
balances: public(uint256[3]) = [100, 200, 300]
let balances: [u64; 3] = [100, 200, 300];
let balances: vector<u64> = vector[100, 200, 300];
Transaction logs
let logs: [&str; 3] = ["log1", "log2", "log3"];
let logs: vector<string> = vector["log1", "log2", "log3"];
Metadata
string[5] public metadata = ["name", "symbol", "decimals", "version", "author"];
metadata: public(string[5]) = ["name", "symbol", "decimals", "version", "author"]
let metadata: [&str; 5] = ["name", "symbol", "decimals", "version", "author"];
let metadata: vector<string> = vector["name", "symbol", "decimals", "version", "author"];
To learn how to build arrays with Solidity, Cyfrin Updraft has a great tutorial to help you learn.