0% found this document useful (0 votes)
4 views

Array

Uploaded by

Neha.Kale K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array

Uploaded by

Neha.Kale K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Array

Array can have a compile-time fixed size or a dynamic size.

// SPDX-License-Identifier: MITpragma solidity ^0.8.26;


contract Array {
// Several ways to initialize an array
uint256[] public arr;
uint256[] public arr2 = [1, 2, 3];
// Fixed sized array, all elements initialize to 0
uint256[10] public myFixedSizeArr;

function get(uint256 i) public view returns (uint256) {


return arr[i];
}

// Solidity can return the entire array.


// But this function should be avoided for
// arrays that can grow indefinitely in length.
function getArr() public view returns (uint256[] memory) {
return arr;
}

function push(uint256 i) public {


// Append to array
// This will increase the array length by 1.
arr.push(i);
}

function pop() public {


// Remove last element from array
// This will decrease the array length by 1
arr.pop();
}

function getLength() public view returns (uint256) {


return arr.length;
}

function remove(uint256 index) public {


// Delete does not change the array length.
// It resets the value at index to it's default value,
// in this case 0
delete arr[index];
}

function examples() external pure {


// create array in memory, only fixed size can be created
uint256[] memory a = new uint256[](5);
}
}

Examples of removing array element


Remove array element by shifting elements from right to left

// SPDX-License-Identifier: MITpragma solidity ^0.8.26;


contract ArrayRemoveByShifting {
// [1, 2, 3] -- remove(1) --> [1, 3, 3] --> [1, 3]
// [1, 2, 3, 4, 5, 6] -- remove(2) --> [1, 2, 4, 5, 6, 6] -->
[1, 2, 4, 5, 6]
// [1, 2, 3, 4, 5, 6] -- remove(0) --> [2, 3, 4, 5, 6, 6] -->
[2, 3, 4, 5, 6]
// [1] -- remove(0) --> [1] --> []

uint256[] public arr;

function remove(uint256 _index) public {


require(_index < arr.length, "index out of bound");

for (uint256 i = _index; i < arr.length - 1; i++) {


arr[i] = arr[i + 1];
}
arr.pop();
}

function test() external {


arr = [1, 2, 3, 4, 5];
remove(2);
// [1, 2, 4, 5]
assert(arr[0] == 1);
assert(arr[1] == 2);
assert(arr[2] == 4);
assert(arr[3] == 5);
assert(arr.length == 4);

arr = [1];
remove(0);
// []
assert(arr.length == 0);
}
}

Inheritance
Solidity supports multiple inheritance. Contracts can inherit other contract by using
the is keyword.

Function that is going to be overridden by a child contract must be declared as virtual.

Function that is going to override a parent function must use the keyword override.

Single inheritance
// Solidity program to
// demonstrate
// Single Inheritance
pragma solidity >=0.4.22 <0.6.0;

// Defining contract
Multi-level Inheritance
// Solidity program to
// demonstrate Multi-Level
// Inheritance
pragma solidity >=0.4.22 <0.6.0;

// Defining parent contract A


contract A {

// Declaring state variables


string internal x;
string a = "Geeks" ;
string b = "For";

// Defining external function


// to return concatenated string
function getA() external{
x = string(abi.encodePacked(a, b));
}
}

// Defining child contract B


// inheriting parent contract A
contract B is A {

// Declaring state variables


// of child contract B
string public y;
string c = "Geeks";

// Defining external function to


// return concatenated string
function getB() external payable returns(
string memory){
y = string(abi.encodePacked(x, c));
}
}

// Defining child contract C


// inheriting parent contract A
contract C is B {

// Defining external function


// returning concatenated string
// generated in child contract B
function getC() external view returns(
string memory){
return y;
}
}

// Defining calling contract


contract caller {

// Creating object of child C


C cc = new C();
// Defining public function to
// return final concatenated string
function testInheritance(
) public returns (
string memory) {
cc.getA();
cc.getB();
return cc.getC();
}
}

Hierarchical Inheritance
// Solidity program to demonstrate
// Hierarchical Inheritance
pragma solidity >=0.4.22 <0.6.0;

// Defining parent contract A


contract A {

// Declaring internal
// state variable
string internal x;

// Defining external function


// to set value of
// internalstate variable
function getA() external {
x = "GeeksForGeeks";
}

// Declaring internal
// state variable
uint internal sum;

// Defining external function


// to set the value of
// internal state variable sum
function setA() external {
uint a = 10;
uint b = 20;
sum = a + b;
}
}

// Defining child contract B


// inheriting parent contract A
contract B is A {

// Defining external function to


// return state variable x
function getAstr(
) external view returns(string memory){
return x;
}

}
// Defining child contract C
// inheriting parent contract A
contract C is A {

// Defining external function to


// return state variable sum
function getAValue(
) external view returns(uint){
return sum;
}
}

// Defining calling contract


contract caller {

// Creating object of contract B


B contractB = new B();

// Creating object of contract C


C contractC = new C();

// Defining public function to


// return values of state variables
// x and sum
function testInheritance(
) public returns (
string memory, uint) {
return (
contractB.getAstr(), contractC.getAValue());
}
}
Multiple Inheritance
// Solidity program to
// demonstrate
// Multiple Inheritance
pragma solidity >=0.4.22 <0.6.0;

// Defining contract A
contract A {

// Declaring internal
// state variable
string internal x;

// Defining external function


// to set value of
// internal state variable x
function setA() external {
x = "GeeksForGeeks";
}
}

// Defining contract B
contract B {

// Declaring internal
// state variable
uint internal pow;

// Defining external function


// to set value of internal
// state variable pow
function setB() external {
uint a = 2;
uint b = 20;
pow = a ** b;

}
}

// Defining child contract C


// inheriting parent contract
// A and B
contract C is A, B {

// Defining external function


// to return state variable x
function getStr(
) external returns(string memory) {
return x;
}

// Defining external function


// to return state variable pow
function getPow(
) external returns(uint) {
return pow;
}
}

// Defining calling contract


contract caller {

// Creating object of contract C


C contractC = new C();

// Defining public function to


// return values from functions
// getStr and getPow
function testInheritance(
) public returns(string memory, uint) {
contractC.setA();
contractC.setB();
return (
contractC.getStr(), contractC.getPow());
}
}

You might also like