-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.java
More file actions
92 lines (73 loc) · 1.72 KB
/
Array.java
File metadata and controls
92 lines (73 loc) · 1.72 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package algorithm.array;
import java.util.ArrayList;
/**
* @Author: zs
* @Date: 2020/11/17 16:48
* 实现一个支持动态扩容的数组
*/
public class Array {
/**
* 存数据
*/
public int[] data;
/**
* 数组长度
*/
public int n;
/**
* 实际大小
*/
public int size;
public Array(int capacity){
this.data = new int[capacity];
this.n = capacity;
this.size=0;
}
public int find(int index){
if(index < 0 || index > size){
return -1;
}
return data[index];
}
public boolean insert(int index, int value){
if(size == n){
System.out.println("数组已满");
return false;
}
if(index < 0 || index > size){
System.out.println("位置不合法");
return false;
}
for (int i = size; i > index ; i--) {
data[i] = data[i-1];
}
data[index] = value;
size++;
return true;
}
public boolean delete(int index){
if(index < 0 || index > size){
return false;
}
if (n - index + 1 >= 0) {
System.arraycopy(data, index + 1, data, index + 1 - 1, n - index + 1);
}
--size;
return true;
}
public void printAll(){
for (int i : data) {
System.out.println(i + " ");
}
System.out.println();
}
public static void main(String[] args) {
Array array = new Array(3);
// array.insert(0, 5);
// array.insert(0, 9999);
// array.insert(1, 111);
// array.insert(2, 222);
// array.insert(3, 333);
array.printAll();
}
}