Dynamic Arrays and its Operations in Solidity
The Dynamic arrays are the arrays that are allocated memory at the runtime and the memory is allocated from the heap.
// declaration of dynamic array
int private arr;
How They Are Different From Fixed Size Arrays?
The fixed-size array has a fixed memory size whereas, in dynamic arrays, the size can be randomly updated during the run time which may be considered efficient with respect to the memory complexity of the code.
Problem: How to create a dynamic array in solidity and perform its associated operations?
Solution: In this article, we will create dynamic arrays in solidity language and will perform the following operations on it:
- Add data in an array
- Get data of an array
- Get length of an array
- Get sum of elements of an array
- Search a particular element in an array
What is Solidity?
Solidity is a high-level language. The structure of smart contracts in solidity is very similar to the structure of classes in object-oriented languages. The solidity file has an extension .sol.
What are Smart Contracts?
Solidity’s code is encapsulated in contracts which means a contract in Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. A contract is a fundamental block of building an application on Ethereum.
Step 1: Open Remix-IDE.
Step 2: Select File Explorer from the left side icons and select Solidity in the environment. Click on the New option below the Solidity environment. Enter the file name as dynamicArray.sol and Click on the OK button.
Step 3: Enter the following Solidity Code. Select the same solidity version as in your code.
Step 4: Compile the file dynamicArray.sol from the Solidity Compiler tab.
Step 5: Deploy the smart contract from the Deploy and Run Transaction tab.
Step 6: Perform various operations on the array under the deployed contracts section.