In Solidity, an array is a collection of elements of the same data type. Arrays can be of fixed size or dynamic size, depending on how they are declared. They are useful for storing multiple values in a single variable, making it easier to manage collections of data.

Types of Arrays

  • Fixed-size Arrays: The size of the array is defined at the time of declaration and cannot be changed.
  • Dynamic Arrays: The size of the array can change during the execution of the contract, allowing elements to be added or removed.

Declaring Fixed-size Arrays

To declare a fixed-size array, specify the data type followed by the array size in square brackets.

Sample Code for Fixed-size Arrays


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract FixedSizeArrayExample {
uint256[5] public fixedArray; // A fixed-size array of 5 unsigned integers

// Function to set values in the fixed-size array
function setValues(uint256[5] memory values) public {
fixedArray = values; // Assign the input values to the fixed array
}

// Function to get a value at a specific index
function getValue(uint256 index) public view returns (uint256) {
return fixedArray[index]; // Return the value at the specified index
}
}

Declaring Dynamic Arrays

To declare a dynamic array, specify the data type followed by empty square brackets.

Sample Code for Dynamic Arrays


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract DynamicArrayExample {
uint256[] public dynamicArray; // A dynamic array of unsigned integers

// Function to add a value to the dynamic array
function addValue(uint256 value) public {
dynamicArray.push(value); // Add the value to the end of the array
}

// Function to get the length of the dynamic array
function getLength() public view returns (uint256) {
return dynamicArray.length; // Return the length of the array
}

// Function to get a value at a specific index
function getValue(uint256 index) public view returns (uint256) {
require(index < dynamicArray.length, "Index out of bounds"); // Ensure index is valid
return dynamicArray[index]; // Return the value at the specified index
}
}

Multidimensional Arrays

Solidity also supports multidimensional arrays, which can be either fixed-size or dynamic.

Sample Code for Multidimensional Arrays


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MultiDimensionalArrayExample {
uint256[2][3] public fixedMultiArray; // A fixed-size 2D array (3 rows, 2 columns)

// Function to set values in the fixed multidimensional array
function setMultiArray(uint256[2][3] memory values) public {
fixedMultiArray = values; // Assign the input values to the fixed multidimensional array
}

// Function to get a value at a specific row and column
function getMultiValue(uint256 row, uint256 col) public view returns (uint256) {
return fixedMultiArray[row][col]; // Return the value at the specified row and column
}
}

Conclusion

Arrays in Solidity are essential for managing collections of data. They can be fixed-size or dynamic, allowing developers to choose the appropriate structure based on their needs. By using arrays, developers can efficiently store and manipulate multiple values within their smart contracts, enhancing the overall functionality and organization of the code.