0
0
Blockchain / Solidityprogramming~10 mins

Arrays (fixed and dynamic) in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Arrays (fixed and dynamic)
Start
Declare Array
Fixed Size?
YesAllocate fixed space
Use fixed array
No
DynamicAllocate flexible space
Use dynamic array
Access/Modify
End
This flow shows how arrays are declared as fixed or dynamic, allocated accordingly, then used for access or modification.
Execution Sample
Blockchain / Solidity
fixedArray = [10, 20, 30]
dynamicArray = []
dynamicArray.push(40)
dynamicArray.push(50)
print(fixedArray[1])
print(dynamicArray[0])
This code creates a fixed array with 3 elements and a dynamic array where elements are added, then prints specific elements.
Execution Table
StepActionArray StateOperation DetailOutput
1Declare fixedArray[10, 20, 30]Fixed size array with 3 elements allocated
2Declare dynamicArray[]Empty dynamic array created
3dynamicArray.push(40)[40]Added 40 to dynamicArray
4dynamicArray.push(50)[40, 50]Added 50 to dynamicArray
5print(fixedArray[1])[10, 20, 30]Access element at index 120
6print(dynamicArray[0])[40, 50]Access element at index 040
7End[10, 20, 30], [40, 50]Program finished
💡 Program ends after printing requested elements from both arrays.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
fixedArrayundefined[10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
dynamicArrayundefinedundefined[][40][40, 50][40, 50]
Key Moments - 3 Insights
Why can't we add more elements to fixedArray after declaration?
Because fixedArray is a fixed size array allocated with a set length (3 elements). The execution_table rows 1 and 3 show dynamicArray can grow, but fixedArray stays the same size.
How does dynamicArray grow when we push elements?
dynamicArray starts empty (row 2) and each push adds an element (rows 3 and 4), increasing its size dynamically.
What happens if we try to access an index outside the array length?
Accessing an invalid index would cause an error or return undefined. In the execution_table, only valid indices are accessed (rows 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of dynamicArray after step 3?
A[10, 20, 30]
B[40]
C[]
D[40, 50]
💡 Hint
Check the 'Array State' column at step 3 in execution_table.
At which step does fixedArray get its values assigned?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column for fixedArray declaration in execution_table.
If we try to push an element to fixedArray, what would happen?
AIt will cause an error or fail because fixedArray size is fixed.
BIt will replace the last element.
CIt will add the element successfully.
DIt will convert fixedArray to dynamic.
💡 Hint
Refer to key_moments about fixedArray size and execution_table rows showing dynamicArray growing but fixedArray not.
Concept Snapshot
Arrays store multiple values in order.
Fixed arrays have a set size and cannot grow.
Dynamic arrays can grow by adding elements.
Access elements by index starting at 0.
Fixed arrays allocate fixed space; dynamic arrays allocate flexible space.
Use push() to add elements to dynamic arrays.
Full Transcript
This lesson shows how arrays work in blockchain programming. We start by declaring a fixed array with three numbers. This array cannot change size. Then we create a dynamic array that starts empty. We add two numbers to it using push. We print the second element of the fixed array and the first element of the dynamic array. The fixed array stays the same size, while the dynamic array grows as we add elements. Accessing elements uses their position number, starting at zero. Trying to add elements to a fixed array will cause an error because its size is fixed.