-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayExample.sol
40 lines (33 loc) · 1.12 KB
/
ArrayExample.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ArrayExample {
// Declare a dynamic array of unsigned integers
uint[] public numbers;
// Add a number to the array
function addNumber(uint _number) public {
numbers.push(_number);
}
// Get the number at a specific index
function getNumber(uint _index) public view returns (uint) {
require(_index < numbers.length, "Index out of bounds");
return numbers[_index];
}
// Update the number at a specific index
function updateNumber(uint _index, uint _newNumber) public {
require(_index < numbers.length, "Index out of bounds");
numbers[_index] = _newNumber;
}
// Remove the last number in the array
function removeLastNumber() public {
require(numbers.length > 0, "Array is empty");
numbers.pop();
}
// Get the length of the array
function getArrayLength() public view returns (uint) {
return numbers.length;
}
// Get all the numbers in the array
function getAllNumbers() public view returns (uint[] memory) {
return numbers;
}
}