Dynamic Arrays and its Operations in Solidity
Last Updated :
17 Nov, 2020
The Dynamic arrays are the arrays that are allocated memory at the runtime and the memory is allocated from the heap.
Syntax:
// 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.
Solidity
// Solidity program to demonstrate
// the above approach
pragma solidity ^0.6.8;
contract DynamicArray{
// Declaring state variable
int[] private arr;
// Function to add data
// in dynamic array
function addData(int num) public
{
arr.push(num);
}
// Function to get data of
// dynamic array
function getData() public view returns(int[] memory)
{
return arr;
}
// Function to return length
// of dynamic array
function getLength() public view returns (uint)
{
return arr.length;
}
// Function to return sum of
// elements of dynamic array
function getSum() public view returns(int)
{
uint i;
int sum = 0;
for(i = 0; i < arr.length; i++)
sum = sum + arr[i];
return sum;
}
// Function to search an
// element in dynamic array
function search(int num) public view returns(bool)
{
uint i;
for(i = 0; i < arr.length; i++)
{
if(arr[i] == num)
{
return true;
}
}
if(i >= arr.length)
return false;
}
}
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.
Similar Reads
Mathematical Operations in Solidity Solidity is a brand-new programming language created by the Ethereum which is the second-largest market of cryptocurrency by capitalization, released in the year 2015 led by Christian Reitwiessner. Ethereum is a decentralized open-source platform based on blockchain domain, used to run smart contrac
6 min read
Static and Dynamic Linking in Operating Systems Static Linking: When we click the .exe (executable) file of the program and it starts running, all the necessary contents of the binary file have been loaded into the process's virtual address space. However, most programs also need to run functions from the system libraries, and these library funct
3 min read
Dynamic Attributes in Python Dynamic attributes in Python are terminologies for attributes that are defined at runtime, after creating the objects or instances. In Python we call all functions, methods also as an object. So you can define a dynamic instance attribute for nearly anything in Python. Consider the below example for
2 min read
How do Dynamic arrays work? A Dynamic array (vector in C++, ArrayList in Java) automatically grows when we try to make an insertion and there is no more space left for the new item. Usually the area doubles in size. A simple dynamic array can be constructed by allocating an array of fixed-size, typically larger than the number
15+ min read
Solidity - Arrays Arrays are data structures that store the fixed collection of elements of the same data types in which each and every element has a specific location called index. Instead of creating numerous individual variables of the same type, we just declare one array of the required size and store the element
6 min read