Array1.sol
unknown
plain_text
5 years ago
2.6 kB
5
Indexable
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.7.5;
contract Array {
uint[] intergerArray; //sample showing initialization of an array of integers
bool[] boolArray; //sample showing initialization of an array of booleans
address[] addressArray; //sample showing initialization of an array of address etc.
uint[] myArray = new uint[](7);
function manipulateArray() external {
//uint[5] storage myArray ;
myArray.push(1) ; // add an element to the array
myArray.push(2) ; // add another element to the array
myArray [0] ; // get the element at key 0
myArray [0] = 20 ; // update the first element in the array
// we can also get the element in the array using the for loop
for (uint j = 0 ; j < 3; j++){
myArray[j];
}
}
}
contract MemoryArray {
/*
* These are temporary arrays and only exist when you are executing a function
* must have a fixed size and must be declared inside a function
* you cannot run a for loop in a memory array
*/
constructor () {
uint [] memory newArray = new uint[] (10) ;
newArray[0] = 1 ;
newArray[1] = 2 ;
}
function testArray() public {
uint len = 7;
//dynamic array
uint[] memory a = new uint[](7);
//bytes is same as byte[]
bytes memory b = new bytes(len);
assert(a.length == 7);
assert(b.length == len);
//access array variable
a[6] = 8;
//test array variable
assert(a[6] == 8);
//static array
uint[3] memory c = [uint(1) , 2, 3];
assert(c.length == 3);
}
uint[] myArray ;
function Test ( uint num) public {
myArray.push (4) ;
myArray.push (5) ;
myArray.push (6) ;
}
function getfirstElement ( uint Point) public returns (uint){
return myArray [0] ;
}
function getLenght () public returns ( uint) {
return myArray.length ;
}
//Solidity program to demonstrate
//creating a fixed-size array
//Creating a contract
// Declaring state variables of type array
uint[6] data1;
//Defining function to add
//values to an array
function array_example() public returns (
int[5] memory, uint[6] memory){
int[5] memory data
= [int (50), -63, 77, -28, 90];
data1
= [uint (10), 20, 30, 40, 50, 60];
return (data, data1);
}
}Editor is loading...