Working with Fixed and Dynamic Arrays in Solidity
📖 Scenario: You are building a simple smart contract to manage a list of favorite numbers. Some numbers are fixed and cannot be changed, while others can be added dynamically by users.
🎯 Goal: Create a Solidity contract that uses both fixed-size and dynamic arrays to store numbers. You will initialize a fixed array, set a limit for adding numbers dynamically, add numbers to the dynamic array, and finally display all stored numbers.
📋 What You'll Learn
Create a fixed-size array called
fixedNumbers with exactly 3 numbers: 10, 20, and 30Create a dynamic array called
dynamicNumbers to store unsigned integersCreate a variable called
maxDynamicNumbers set to 5 to limit additions to the dynamic arrayWrite a function
addNumber(uint number) that adds a number to dynamicNumbers only if it has less than maxDynamicNumbers elementsWrite a function
getAllNumbers() that returns both arrays combined as a single array💡 Why This Matters
🌍 Real World
Smart contracts often need to store fixed and changing lists of data, like token balances or user inputs.
💼 Career
Understanding arrays in Solidity is essential for blockchain developers building decentralized applications.
Progress0 / 4 steps