Table of Contents

What are arrays?

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.

General structure of arrays

An array consists of three components:

  • Element type: For statically typed languages (like C++, Java, C#) elements in an array are typically the same type. Dynamically typed languages (like Python, JavaScript, Ruby) allow types to be mixed. An array can store integers, addresses, strings, booleans,or custom types.
  • Index: Arrays use zero-based indexing, meaning the first element is at index 0, the second at index 1, and so on to n-1, given there are n elements. This allows for quick access to any element using its index.
  • Size: Arrays can be fixed-size, where the number of elements is determined upon declaration, or dynamic, where elements can be added and removed.
Visual representation of an array.

Example: Fixed-size array of integers in C++

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
    • If no value is assigned to an index, the array will store a default value based on the element type, 0 in this case.
    • By default, regular arrays of local scope (declared within a function) are uninitialized. None of its elements are set to any particular value, meaning contents are undetermined at the point the array is declared.
    • Elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example:
// initializing values in braces
int myArray[5] = {0, 1, 2, 3, 4};

Example: Dynamic array of strings in C#

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.

What languages use arrays?

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.

Arrays in smart contract development 

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.

What are the advantages of using arrays?

  • Efficient data storage: Arrays store elements in contiguous memory locations, which allows for efficient access and manipulation.
  • Fast indexing: Using index-based retrieval, arrays allow constant-time (O(1)) access to elements. Retrieving an array’s element is executed in a fixed amount of time, regardless of the array's size. In technical terms, this is known as O(1) time complexity.
  • Scalability: Arrays can handle large datasets as found in decentralized finance (DeFi) platforms or token distribution systems.
  • Gas optimization: In blockchain environments, efficient use of storage directly impacts gas costs. Arrays allow developers to optimize storage, reducing the computational cost of smart contract operations.

What kinds of data are stored in arrays?

Arrays can store various data types, depending on the requirements of a decentralized application or smart contract. Here are a few examples:

Addresses

  • Solidity: address[] public users;
  • Vyper: users: public(address[5])
  • Rust: let users: [String; 3] = ["0x123", "0x456", "0x789"];
  • Move: let users: vector<address> = vector[0x123, 0x456, 0x789];

Token balances

  • Solidity: uint256[] public balances = [100, 200, 300];
  • Vyper: balances: public(uint256[3]) = [100, 200, 300]
  • Rust: let balances: [u64; 3] = [100, 200, 300];
  • Move: let balances: vector<u64> = vector[100, 200, 300];

Transaction logs

  • Rust: let logs: [&str; 3] = ["log1", "log2", "log3"];
  • Move: let logs: vector<string> = vector["log1", "log2", "log3"];

Metadata

  • Solidity: string[5] public metadata = ["name", "symbol", "decimals", "version", "author"];
  • Vyper: metadata: public(string[5]) = ["name", "symbol", "decimals", "version", "author"]
  • Rust: let metadata: [&str; 5] = ["name", "symbol", "decimals", "version", "author"];
  • Move: 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.

Related Terms

No items found.