[ad_1]
- groups of value type of the same type [10,20,30,40,50]
– fixed-size array uint[5] myArray
-dynamic array uint[ ] myArray
-one dimensional array
-multi dimensional array
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myarray{uint[6] ipadPrice=[1000,2000,3000,4000,5000,1000]; // fixed size
integer array with 6 elementsuint[] macPrice=[1100,2100,3100]; // dynamic arrayuint[2] ipadProPrice=[1500,2000]; //one dimensional fixed size arrayuint[] macProPrice=[2000,2500,3000]; //one dimensional dynamic arrayuint[2][2] ipadAirPrice; // multi dimensional fixed-size arrayuint[][] ipad8Price; //multi dimensional dynamic arrayuint[2][] iphone8Price; // multi dimensional mixed siza arrayuint[][2] iphone11Price; // multi dimensional mixed siza arrayuint[][][][] macBook; // four multi dimensional dynamic array upto15string[] macbook2; // array of stringbool[] macbook3; // array of bool
}
When use different type in array
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint[] error1=[1,true,"ahmed"];
//Unable to deduce common type for array elements
}
Add value to dimensional array
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint[][]ipadAirPrice=[[uint(1200),1400,1600,1800],[uint(8),6,8,4]];}
Notice how it’s necessary to manually cast the first elements of the sub-arrays to a common type, because Solidity tries to use the small numbers you have in the array (8, 6, 8 4) as uint8
and the big numbers (1200, 1400, etc) as the next type that can hold them, which would be uint16
. Since uint8
and uint16
are not compatible, you need to cast at least the first element of each sub-array to a common element so you let Solidity know what is the type of each of those sub-elements.Solidity compiler is not yet smart enough to handle that. Maybe in the future, it will.
- push () accepts an element and add to the end of array
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint[] public ipadProPrice=[1000,1100,1200,1300,1400,1500];
string [] public appleProduct=["ipad","macbook","iphone"];function addToPrice(uint addIpadPrice) public{
ipadProPrice.push(addIpadPrice);
}function addToAplle(string memory newAplleProduct) public{
appleProduct.push(newAplleProduct);
}function addToPrice() public{
ipadProPrice.push()=5;
}function addToAplle() public{
appleProduct.push()="ipadAir";
}
}
You can’t push to a fixed size array because it has been fixed already, and memory has already been allocated to it. If you want to push, you need to use dynamic arrays. Dynamic arrays have no fixed size, can contain numbers, and keep growing.
pragma solidity >=0.7.1 <0.9.0;
contract myArray{
uint[] public ipadProPrice =[1000,1100,1200,1300,1400,1500];function addToPrice(uint _addToPrice) public{
ipadProPrice.push(_addToPrice);
}
}
- Length Arrays have length member that contains that elements
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint[] public ipadProPrice=[1000,1100,1200,1300,1400,1500];
string [] public appleProduct=["ipad","macbook","iphone"];function getLength() public view returns(uint){return ipadProPrice.length;
}function getLengthS() public view returns(uint){return appleProduct.length;
}
}
- pop() items can be removed from the array using pop method which remove the last index and adjust the length
- pop method in dynamic array only
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint[] public ipadProPrice=[1000,1100,1200,1300,1400,1500];
string [] public appleProduct=["ipad","macbook","iphone"];function getLength() public view returns(uint){
return ipadProPrice.length;
}function getLengthS() public view returns(uint){
return appleProduct.length;
}function popMethod() public{
ipadProPrice.pop();
appleProduct.pop();
}
}
- Delete member not delete the index (index to zero)
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint[] public ipadProPrice=[1000,1100,1200,1300,1400,1500];
string [] public appleProduct=["ipad","macbook","iphone"];function deleteMethod() public {
delete ipadProPrice[1];
}
}
- you need to specify the index between square brackets
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint[] ipadProPrice=[1000,1100,1200,1300,1400,1500];
string [] appleProduct=["ipad","macbook","iphone"];uint public accessElement=ipadProPrice[5];
string public accessElementS=appleProduct[1];
}// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint[] ipadProPrice=[1000,1100,1200,1300,1400,1500];
string [] appleProduct=["ipad","macbook","iphone"];function getIpadPrice() public view returns(uint){
return ipadProPrice[2];
}function getAppleProduct() public view returns( string memory){
return appleProduct[1];
}
}
access elements in multi dimensional array
// SPDX-License-Identifier: GPL-3.0pragma solidity >=0.7.1 <0.9.0;
contract myArray{uint [][] ipadPriceRam =[[uint(1000),1200,1300],[uint(8),16,32]];string [][] appleProduct=[["ipad","macbook","iphone"],["airpod","typec","headphone"]];function getIpadPriceRam() public view returns(uint){
return ipadPriceRam[1][2];
}function appleProductAccess() public view returns(string memory){
return appleProduct[1][0];
}function accessTwoArray() public view returns(uint,uint){
return(ipadPriceRam[0][0],ipadPriceRam[1][0]);
}
}
bytes array
-variable of bytes and string are special array
-bytes is dynamic array can hold any number of bytes
-bytes4 fixed size array / much cheaper
-bytes is similar to bytes[] but bytes is cheaper
-you can run length and pop operation only
-you can not run push operation
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract specialArray{bytes public appleProduct= "ipad"; //any nunmber of bytes
bytes4 public newApple="ipad"; // four bytes only
bytes32 public name="mahmoud ibrahim elsayed"; //upto 32 bytesfunction access() public view returns(bytes1){ // index in array
return appleProduct[1];
}function getLength() public view returns(uint){ // get length
return appleProduct.length;
}function popArray()public{
return appleProduct.pop();
}
}
- dynamic data type that are based on dynamic
-string not allow length
-string not allow index access
-string not have manipulation function but there are third-party string libraries you can also compare two string by their keccak256 hash
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract stringArray {
string apple="ipadPro";function accessString(uint _index) public view returns(string memory) { //Index access for string is not possiblereturn apple[_index];
}
}
String dont have length property ,it should be converted in to bytes
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract stringArray {string apple="ipadPro";
function getLength() public view returns(uint){ //error
return apple.length;
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract stringArray {string apple="ipadPro";
bytes stringToBytes=bytes(apple);function getLength() public view returns(uint){ //error
return stringToBytes.length;
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract stringCompare {function compareString(string memory _firstName) public pure returns(bool) {if(keccak256(abi.encodePacked(_firstName))==keccak256(abi.encodePacked("mourad"))){
return true;
}else{
return false;
}
}
}
The array [1,-1] is invalid because the type of first index is uint8 while the type of second index int8 ,to make it work you can use [int8(1),-1]
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract arrayOutput {uint [] ipadPrice=[1000,2000,3000];function outputArray()public view returns(uint[] memory){
return ipadPrice;
}
}
How to get the largest element in an array — Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract arrayOutput {uint [] ipadPrice=[1100,1200,1300,1400,1500];
function getMaxPric() public view returns(uint){uint i;
uint maxPrice=0;for(i=0;i<5;i++){
if(maxPrice<ipadPrice[i]){
maxPrice=ipadPrice[i];
}
}
return maxPrice;
}
}
Multidimensional arrays in solidity and mapping
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract arrayMapping{mapping(address => string[][]) getInfo;function addNameJop(string memory name, string memory jop) public{
getInfo[msg.sender].push([name, jop]);
}
function getNameJop() public view returns(string [][] memory){
return getInfo[msg.sender];
}
}
Array example contract to check the price in array
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract ipadPrice{uint256 [] ipadPriceInShop=[1000,2000,5000,4000,6000,10001,3000,250000];function checkIpadPrice(uint thePrice) public view returns(bool){
bool trueprice = false;
for(uint i=0;i<7;i++){
if(ipadPriceInShop[i]== thePrice){
trueprice=true;
}
}
return trueprice;
}
}
Array example check true name -Compare two string
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract forLoopTest{string[] className=["ahmed","mourad","mahmoud","hamza"];
function findclassName(string memory checkName) public view returns (bool){bool isCheckNamecorrect=false;
for(uint i=0;i<4;i++){if(keccak256(abi.encodePacked(className[i]))==keccak256(abi.encodePacked(checkName))){
isCheckNamecorrect=true;
}
}
return isCheckNamecorrect;
}
}
Array example
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract MyContract {
uint256[] data;function write(uint256 entry) public {
data.push(entry);
}function read() public view returns(uint256[] memory) {
return(data);
}
}
New to trading? Try crypto trading bots or copy trading
[ad_2]
Source link