-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.sol
More file actions
40 lines (32 loc) · 985 Bytes
/
array.sol
File metadata and controls
40 lines (32 loc) · 985 Bytes
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.15;
//动态数组
contract ArrayTest {
uint[] arr1;
//动态数组push
function push(uint n) public {
arr1.push(n);
}
//删除最后一个
function pop() public {
arr1.pop();
}
//删除指定索引元素
function deleteElement(uint index) public {
delete arr1[index];
}
function edit(uint index,uint value) public {
arr1[index] = value;
}
function getArr1() public view returns (uint,uint[] memory){
return (arr1.length,arr1);
}
//动态数组中的元素,如果被删除,长度是不变的,现需要实现,删除元素后,数组长度也发生改变
function remove(uint index) public {
require(index < arr1.length,'index out of bound');
for(uint i = index;i<arr1.length-1;i++){
arr1[i] = arr1[i+1];
}
arr1.pop();
}
}