-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn-arrays.sol
67 lines (51 loc) · 2.25 KB
/
learn-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.7.0 < 0.9.0;
// WHAT ARE ARRAYS? :)
// Array is a data structure, which stores a fixed-size sequential collection of elements of the same type.
//An array is used to store a collection of data,
// but it is often more useful to think of an array as a collection of variables of the same type.
// 1. How To Write An Array
// 2. pop, push, and length methods
// 3. remove elements from array
contract learnArrays {
uint[] public myArray; /// add 1 2 3 4
uint[] public myArray2;
uint[200] public myFixedSizedArray;
// The push() method adds one or more elements to the end of an array and returns the new length of the array.
function push(uint number) public {
myArray.push(number);
}
// The pop method removes the last element from an array and returns that value to the caller.
function pop() public {
myArray.pop();
}
// length is a string property that is used to determine the length of a string
function getlength() public view returns (uint) {
return myArray.length;
}
function remove(uint i) public {
delete myArray[i];
// when you delete in your array the length remains the same
// i = 0 = 1, 2 = 1, 3 = 2
}
// Exercise create a function that can fully remove an item from an array
// 1. Create an Empty array called changeArray
// 2. Create a function called removeElement which sets the index argument of the array to the last element in the array
// 3. remove the last index from that function with the pop method
// 4. Create a function called test which pushes 1 2 3 4 into changeArray
// 5. remove the element 2 from the array when the contract is called
uint[] public changeArray;
function removeElement(uint i) public {
changeArray[i] = changeArray[changeArray.length - 1];
changeArray.pop();
}
function test() public {
changeArray.push(1);
changeArray.push(2);
changeArray.push(3);
changeArray.push(4);
}
function getArray() public view returns (uint) {
return changeArray.length;
}
}