-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path06_Arrays.sol
49 lines (39 loc) · 1.43 KB
/
06_Arrays.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract Arrays {
//1. storage arrays --> They stay in the blockchain after exiting the function
//2. memory arrays --> They are destroyed after exiting the function
// storage arrays (dynamic size array)
uint[] myArray;
// fixed size array (you lose convenience method "push")
uint[2] fixedArray;
function playingWithArrays() external {
// adding data to array
myArray.push(1);
myArray.push(2);
// reading value at specific index
myArray[1];
// setting value at specific index
myArray[0] = 25;
// resetting value at specific index
// Note that this doesn't change the length of the array, but it just resets the value to the default value
delete myArray[1]; // resets the int value to 0
for(uint i = 0; i < myArray.length; i++) {
myArray[i];
}
}
// memory arrays: Not saved to the blockchain after exiting function
function memoryArrays() pure internal {
// Memory array with size 10
uint[] memory memoryArray = new uint[](10);
// setting value at index
memoryArray[0] = 10;
// reading value at index
memoryArray[0];
// resetting value at index
delete memoryArray[0];
}
// arrays in functions
function arrayFunction(uint[] memory myArg) internal returns(uint[] memory) {
}
}