Arrays in Solidity

Solidity Programming Language
2 min readFeb 9, 2022
veridelisi.medium.com

Aim: To learn arrays in Solidity

Code:

Dynamic Arrays:

//Dynamic Array

uint[] public darray;

// Dynamic Array with inicial values: darray2

uint[] public darray2=[0,1,2,3];

// Dynamic Array with inicial “0” values: darray3

uint[] public darray3=new uint[](4);

Fixed Arrays:

//Fixed Array

uint[3] public farray;

//Fixed Array with values : farray2

uint[3] public farray2=[uint(1),2,3];

Dynamic Array

Add to element to the dynamic arrays

function pushDarray(uint item) public {

darray.push(item);

}

function pushDarray2(uint item) public {

darray2.push(item);

}

function pushDarray3(uint item) public {

darray3.push(item);

}

Dynamic Array

Update the element in dynamic arrays

function updateDarray(uint index, uint value) public {

darray[index] = value;

}

function updateDarray2(uint index,uint value) public {

darray2[index] = value;

}

function updateDarray3(uint index,uint value) public {

darray3[index] = value;

}

Fixed Array

Update the element in fixed arrays

function updateFarray( uint index, uint value) public {

farray[index]=value;

}

function updateFarray2(uint index, uint value) public {

farray2[index]=value;

}

Dynamic Array

See all the elements of dynamic arrays

function seeDarray() public view returns (uint[] memory) {

return darray;

}

function seeDarray2() public view returns(uint[] memory){

return darray2;

}

function seeDarray3() public view returns(uint[] memory){

return darray3;

}

Fixed Array

See all the elements of fixed arrays

function seeFarray() public view returns (uint[3] memory){

return farray;

}

function seeFarray2() public view returns (uint[3] memory){

return farray2;

}

Dynamic Array

Delete the last element of dynamic arrays

function deleteDarray() public {

darray.pop();

}

function deleteDarray2() public {

darray2.pop();

}

function deleteDarray3() public {

darray3.pop();

}

--

--

Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps