-
Notifications
You must be signed in to change notification settings - Fork 0
/
practiceWithArrays
53 lines (43 loc) · 1.62 KB
/
practiceWithArrays
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
50
51
52
53
pragma solidity ^0.8.0;
// Define a new contract
contract practiceWithArrays {
// Declare public arrays of unsigned integers
uint[] public myArray;
uint[] public myArray2;
// Declare a public array of unsigned integers with a fixed length of 200
uint[200] public myFixedLengthArray;
// Define a function to add a number to the end of myArray
function push(uint number) public {
myArray.push(number);
}
// Define a function to remove the last element from myArray
function pop() public {
myArray.pop();
}
// Define a function to get the length of myArray
function getLength() public view returns (uint) {
return myArray.length;
}
// Define a function to remove an element from myArray at a specific index
function remove(uint i) public {
delete myArray [i];
}
// Declare another public array of unsigned integers
uint[] public changeArray;
// Define a function to remove an element from changeArray at a specific index
// This function replaces the element to be removed with the last element in the array, then removes the last element
function removeElement(uint i) public {
changeArray[i] = changeArray[changeArray.length - 1];
changeArray.pop();
}
// Define a function to add the numbers 1 through 4 to changeArray
function test() public {
for(uint i = 1; i <=4; i++) {
changeArray.push(i);
}
}
// Define a function to get all elements in changeArray
function getChangeArray() public view returns(uint[] memory) {
return changeArray;
}
}