我是靠谱客的博主 现实小笼包,这篇文章主要介绍自定义动态数组,现在分享给大家,希望可以做个参考。

自定义动态数组的基本操作:

1、定义一个数组(静态数组)
2、向指定位置添加元素e
3、获取指定索引位置的元素
4、修改指定索引位置的元素为e
5、查找是否包含元素e
6、查找数组中元素e的索引
7、删除指定索引位置的元素
8、动态数组:扩容(防止复杂度震荡)

复制代码
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/** * 数组:动态数组,泛型 * * @author f242 * @since V1.0.0 * 2020-03-19 15:26 */ public class Array<E> { private E[] data; private int size; /** * 构造函数:传入数组的容量capacity构造Array * @param capacity */ public Array(int capacity){ data = (E[])new Object[capacity]; size =0; } /** * 无参构造函数,默认数组容量capacity=10 */ public Array(){ this(10); } /** * 获取数组中元素个数 * @return */ public int getSize(){ return size; } /** * 获取数组容量 * @return */ public int getCapacity(){ return data.length; } /** * 返回数组是否为空 * @return */ public boolean isEmpty(){ return size ==0; } /** * 向所有元素最后添加一个元素 * @param e */ public void addLast(E e){ add(size,e); } /** * 向所有元素前添加一个元素e * @param e */ public void addFirst(E e){ add(0,e); } /** * 在第index的位置上插入一个新元素e * @param index * @param e */ public void add(int index,E e){ if(index<0 || index>size){ throw new IllegalArgumentException("Add falied,index is a illegal number"); } if(size ==data.length){//扩容 resize(data.length*2); } //从后面开始移动赋值 for(int i=size-1; i>=index;i--){ data[i+1] = data[i]; } data[index]= e; size ++; } /** * 获取index索引位置的元素 * @param index * @return */ public E get(int index){ if(index<0 || index >= size){ throw new IllegalArgumentException("Get falied, index is illeagl"); } return data[index]; } /** * 获取第一个元素 * @return */ public E getFirst(){ return get(0); } /** * 获取最后一个元素 * @return */ public E getLast(){ return get(size-1); } /** * 修改index索引位置的元素为e * @param index * @param e */ public void set(int index,E e){ if(index<0 || index >= size){ throw new IllegalArgumentException("Set failed,index is illlegal"); } data[index] = e; } /** * 查找数组中是否有元素e * @param e * @return */ public boolean contains(E e){ for(int i=0;i<size; i++){ if(data[i].equals(e)){ return true; } } return false; } /** * 查找数组中元素e的索引,如果不存在返回-1 * @param e * @return */ public int find(E e){ for(int i=0;i<size;i++){ if(data[i].equals(e)){ return i; } } return -1; } /** * 从数组中删除index位置的元素,返回删除的元素 * @param index * @return */ public E remove(int index){ if( index<0 ||index>= size){ throw new IllegalArgumentException("Remove falied,index is illegal"); } E ret = data[index]; for(int i=index+1;i<size;i++){ data[i-1] = data[i]; } size --; // data[size] =null; //loitering objects != memory leak if(size == data.length/4 && data.length / 2 !=0){//动态数组,防止复杂度震荡 resize(data.length / 2); } return ret; } /** * 从数组中删除第一个元素,返回删除的元素 * @return */ public E removeFirst(){ return remove(0); } /** * 从数组中删除最后一个元素,返回删除的元素 * @return */ public E removeLast(){ return remove(size-1); } /** * 从数组中删除元素e * @param e */ public void removeElement(E e){ int index = find(e); if(index != -1){ remove(index); } } @Override public String toString(){ StringBuffer res = new StringBuffer(); res.append(String.format("Array:size = %d, capacity = %dn",size,data.length)); res.append("["); for(int i=0;i<size;i++){ res.append(data[i]); if(i != size-1){ res.append(","); } } res.append("]"); return res.toString(); } /** * 动态数组:扩容 * @param newCapacity */ private void resize(int newCapacity){ E[] newData = (E[])new Object[newCapacity]; for(int i=0;i<size;i++){ newData[i] = data[i]; } data = newData; } }

最后

以上就是现实小笼包最近收集整理的关于自定义动态数组的全部内容,更多相关自定义动态数组内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部