Arrays help you store many values in one place. Fixed arrays have a set size, dynamic arrays can grow or shrink.
Arrays (fixed and dynamic) in Blockchain / Solidity
pragma solidity ^0.8.0; contract ArrayExample { // Fixed size array of 3 unsigned integers uint[3] fixedArray; // Dynamic array of unsigned integers uint[] dynamicArray; // Function to add an element to dynamic array function addElement(uint element) public { dynamicArray.push(element); } // Function to get element from fixed array function getFixedElement(uint index) public view returns (uint) { return fixedArray[index]; } // Function to set element in fixed array function setFixedElement(uint index, uint value) public { fixedArray[index] = value; } }
Fixed arrays have a size set when declared and cannot change.
Dynamic arrays can change size using push() and pop() functions.
uint[2] fixedArray = [10, 20]; // fixedArray has exactly 2 elements: 10 and 20
uint[] dynamicArray; dynamicArray.push(5); dynamicArray.push(15); // dynamicArray now has 2 elements: 5 and 15
uint[] emptyArray; // emptyArray is empty and has length 0
uint[1] singleElementArray; singleElementArray[0] = 42; // fixed array with one element set to 42
This contract shows a fixed array with 3 numbers set in the constructor. It also has a dynamic array where you can add numbers anytime. You can get both arrays as comma-separated strings.
pragma solidity ^0.8.0; contract ArrayDemo { uint[3] fixedArray; uint[] dynamicArray; // Initialize fixed array with values constructor() { fixedArray[0] = 100; fixedArray[1] = 200; fixedArray[2] = 300; } // Add element to dynamic array function addToDynamicArray(uint value) public { dynamicArray.push(value); } // Get fixed array elements as a string function getFixedArray() public view returns (string memory) { return string(abi.encodePacked( uintToString(fixedArray[0]), ", ", uintToString(fixedArray[1]), ", ", uintToString(fixedArray[2]) )); } // Get dynamic array elements as a string function getDynamicArray() public view returns (string memory) { if(dynamicArray.length == 0) { return "Empty"; } string memory result = uintToString(dynamicArray[0]); for(uint i = 1; i < dynamicArray.length; i++) { result = string(abi.encodePacked(result, ", ", uintToString(dynamicArray[i]))); } return result; } // Helper function to convert uint to string function uintToString(uint v) internal pure returns (string memory) { if (v == 0) { return "0"; } uint j = v; uint len; while (j != 0) { len++; j /= 10; } bytes memory bstr = new bytes(len); uint k = len; while (v != 0) { k = k-1; uint8 temp = uint8(48 + v % 10); bstr[k] = bytes1(temp); v /= 10; } return string(bstr); } }
Accessing an index outside the fixed array size causes an error.
Dynamic arrays use more gas when growing, so use fixed arrays if size is known.
Common mistake: forgetting that fixed arrays cannot change size after declaration.
Fixed arrays have a set size and cannot grow or shrink.
Dynamic arrays can change size using push() and pop().
Use arrays to store lists of values in an ordered way.
Practice
What is the main difference between a fixed array and a dynamic array in blockchain smart contracts?
Solution
Step 1: Understand fixed array size
Fixed arrays have a size defined when declared and cannot change during contract execution.Step 2: Understand dynamic array size
Dynamic arrays can grow or shrink by adding or removing elements during contract execution.Final Answer:
Fixed arrays have a set size, dynamic arrays can change size during execution. -> Option AQuick Check:
Fixed vs dynamic size = A [OK]
- Thinking fixed arrays can change size
- Confusing data types with array size
- Assuming dynamic arrays are slower to access
Which of the following is the correct syntax to declare a dynamic array of uint in Solidity?
?
Solution
Step 1: Recall Solidity array syntax
Dynamic arrays are declared with the type followed by empty square brackets:uint[].Step 2: Check each option
uint[] numbers; uses correct syntax:uint[] numbers;. uint numbers[]; places brackets after variable name, which is invalid. uint numbers(); uses parentheses, which is wrong. uint[] numbers = new uint[]; tries to initialize without size, which is invalid syntax.Final Answer:
uint[] numbers; -> Option BQuick Check:
Dynamic array syntax = A [OK]
- Placing brackets after variable name
- Using parentheses instead of brackets
- Trying to initialize without size incorrectly
What will be the output of the following Solidity code snippet?
pragma solidity ^0.8.0;
contract Test {
uint[] public numbers;
function addNumbers() public {
numbers.push(10);
numbers.push(20);
numbers.push(30);
}
function getSecond() public view returns (uint) {
return numbers[1];
}
}Assuming addNumbers() is called once before getSecond(), what does getSecond() return?
Solution
Step 1: Understand array after push calls
After callingaddNumbers(), the arraynumberscontains [10, 20, 30].Step 2: Access the element at index 1
Arrays in Solidity are zero-indexed, sonumbers[1]is the second element, which is 20.Final Answer:
20 -> Option DQuick Check:
numbers[1] = 20 [OK]
- Confusing index 1 with first element
- Expecting an error for valid index
- Mixing up push order
Identify the error in this Solidity code snippet that tries to remove the last element from a dynamic array:
pragma solidity ^0.8.0;
contract RemoveLast {
uint[] public data = [1, 2, 3];
function removeLast() public {
data.pop(1);
}
}Solution
Step 1: Check pop() method usage
In Solidity,pop()removes the last element and takes no arguments.Step 2: Identify incorrect call
The code callsdata.pop(1);which is invalid syntax and causes a compilation error.Final Answer:
pop() does not take any arguments; calling pop(1) causes an error. -> Option CQuick Check:
pop() no args = C [OK]
- Passing arguments to pop()
- Thinking pop() removes first element
- Believing pop() is unavailable for dynamic arrays
Given a fixed array uint[3] fixedArr = [5, 10, 15]; and a dynamic array uint[] dynamicArr;, which code snippet correctly copies all elements from fixedArr to dynamicArr in Solidity?
Solution
Step 1: Understand array assignment rules
In Solidity, fixed-size arrays cannot be directly assigned to dynamic arrays.Step 2: Copy elements using a loop
Using a for loop to push each element fromfixedArrintodynamicArris the correct way.Final Answer:
for (uint i = 0; i < fixedArr.length; i++) { dynamicArr.push(fixedArr[i]); } -> Option AQuick Check:
Copy fixed to dynamic with loop = D [OK]
- Trying to assign fixed array directly to dynamic array
- Using push with whole array instead of elements
- Reinitializing dynamic array incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
