-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13Arrays.sol
58 lines (46 loc) · 1.56 KB
/
13Arrays.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
contract Arrays{
// Several ways to initialise an array
// Array declaration is done by 'uint256[]'
uint256[] public arr;
uint256[] public arr2 = [1,2,3,4];
uint[10] public fixedLengthArray;
//function to push an item into array
function Add(uint256 i) public {
arr.push(i);
}
//function to remove last item from the array
function remove_Last_Item() public {
arr.pop();
}
//function to remove an item from the array
function remove(uint256 _index) public {
delete arr[_index];
//delete does not change the length of the array,
//instead it replaces the deleted item with 0
}
//get length of an array
function get_length()public view returns(uint256) {
return arr.length;
}
//function to get the item at specific index number
function get_item(uint x) public view returns (uint) {
return arr[x];
}
//function to return the whole array
function get() public view returns (uint256[] memory){
return arr;
}
//When i try to remove element of particular index using delete keyword,
// zero raplaces the value of item
//So, to get the required array, we should shift the array items and
//pop the last dublicate item
function remove_item(uint256 _index ) public {
require(_index < arr.length,"Length of array should be more");
for(uint i = _index;i < arr.length-1;i++){
arr[i] = arr[i+1];
}
arr.pop();
}
}