Solidity Arrays

Preface

In Solidity, there are two kinds of arrays:

Fixed-size array:
You must specify array length at declaration time.
Example: uint[5] numbers;
Once created, the length is not possible to change.
Dynamic-size array:
No need to specify l…


This content originally appeared on DEV Community and was authored by Loading Blocks

Preface

In Solidity, there are two kinds of arrays:

  1. Fixed-size array:

    You must specify array length at declaration time.

    Example: uint[5] numbers;

    Once created, the length is not possible to change.

  2. Dynamic-size array:

    No need to specify length at declaration.

    Example: uint[] numbers;

    The length can be changed at runtime (grow or shrink).

Most common syntax

uint[5] numbers;                     // fixed size array
uint[] numbers;                      // dynamic array
numbers[i];                          // access by index
uint[] storage refToNumbers = numbers; // storage reference
function f(uint[] memory a) external { ... } // pass memory array
numbers.push(x);                     // append to end
numbers.pop();                       // remove the last element
uint n = numbers.length;             // get length
delete numbers[i];                   // reset value to default
for (uint i = 0; i < numbers.length; i++) { ... } // loop

Memory Arrays

When declaring an array inside a function, you must use the memory keyword.

A memory array always have fixed size, so you cannot call push or pop.

You can use the new keyword to create a memory array based on a variable size.

Example:

function createMemoryArray(uint size) public pure returns (uint[] memory) {
    uint[] memory newArr = new uint[](size);
    return newArr;
}


This content originally appeared on DEV Community and was authored by Loading Blocks


Print Share Comment Cite Upload Translate Updates
APA

Loading Blocks | Sciencx (2025-08-21T13:17:16+00:00) Solidity Arrays. Retrieved from https://www.scien.cx/2025/08/21/solidity-arrays/

MLA
" » Solidity Arrays." Loading Blocks | Sciencx - Thursday August 21, 2025, https://www.scien.cx/2025/08/21/solidity-arrays/
HARVARD
Loading Blocks | Sciencx Thursday August 21, 2025 » Solidity Arrays., viewed ,<https://www.scien.cx/2025/08/21/solidity-arrays/>
VANCOUVER
Loading Blocks | Sciencx - » Solidity Arrays. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/21/solidity-arrays/
CHICAGO
" » Solidity Arrays." Loading Blocks | Sciencx - Accessed . https://www.scien.cx/2025/08/21/solidity-arrays/
IEEE
" » Solidity Arrays." Loading Blocks | Sciencx [Online]. Available: https://www.scien.cx/2025/08/21/solidity-arrays/. [Accessed: ]
rf:citation
» Solidity Arrays | Loading Blocks | Sciencx | https://www.scien.cx/2025/08/21/solidity-arrays/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.