自定义ArrayList实现

1. [代码]首先是接口定义     

?
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
/**
  *
  * @author xzf
  *
  * @param <E>
  */
public interface MyList<E>{
     
         /**
          * return the number of elements in the list.
          *
          * @return the number of elements in the list
          */
         int size();
         
         /**
          * return true if this list contains no element.
          *
          * @return true if this list contains no element
          */
         boolean isEmpty();
         
         /**
          * return true if this list contains the specified element.
          *
          * @param o element whose presence in this list is to be tested
          *
          * @return true if this list contains the specified element
          */
         boolean contains(Object o);
         
         /**
          * return an array containing all the elements in this list in a proper sequence.
          *
          * @return an array containing all the elements in this list in a proper sequence
          */
         Object[] toArray();
         
         /**
          * appends the specified element to the end of the list.
          *
          * @param element to be appended to this list
          *
          * @return true
          */
         boolean add(E element);
         
         /**
          * removes the first occurrence of the specified element from this list,
          * if it is present.
          *
          * @param o element to be removed from this list, if present
          *
          * @return true if this list contains the specified element
          */
         boolean remove(Object o);
         
         /**
          * remove all of the elements from this list.
          * the list should be empty after this call returns.
          */
         void clear();
         
         /**
          * compares the specified object with this list from equality.
          *
          * @param o the object to be compared for equality with this list
          *
          * @return <tt>true</tt> if the specified object is equal to this list
          */
         boolean equals(Object o);
         
         /**
          * returns the hash code value for this list.
          *
          * @return the hash code value for this list
          */
         int hashCode();
         
         /**
          * return the element at the specified position in this list.
          *
          * @param index index of the element to return
          *
          * @return the element at the specified position in this list
          */
         E get( int index);
         
         /**
          * replace the element at the specified position in this list with
          * the specified element.
          *
          * @param index index of the element to replace
          *
          * @param element element to be stored at the specified position
          *
          * @return the element previously at the specified position
          */
         E set( int index, E element);
         
         /**
          * insert the specified element at the specified position in this list.
          *
          * @param index index at which the specified element is to be inserted
          *
          * @param element element to be inserted
          */
         void add( int index, E element);
         
         /**
          * remove the element at the specified position in this list.
          *
          * @param index the index of the element to be removed
          *
          * @return the element previously at the specified position
          */
         E remove( int index);
         
         /**
          * return the index of the first occurrence of the specified element
          * in this list, or -1 if this list does not contain the element.
          *
          * @param o element to search for
          *
          * @return the index of the first occurrence of the specified element in
          *         this list, or -1 if this list does not contain the element
          */
         int indexOf(Object o);
         
         /**
          * return the index of the last occurrence of the specified element
          * in this list, or -1 if this list does not contain the element.
          *
          * @param o element to search for
          *
          * @return the index of the first occurrence of the specified element in
          *         this list, or -1 if this list does not contain the element
          */
         int lastIndexOf(Object o);
}

2. [代码]自定义ArrayList实现类     

?
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import java.util.Arrays;
 
public class MyArrayList<E> implements MyList<E> {
     
     /**
      * the array buffer into which the elements of the ArrayList are stored.
      * the capacity of the ArrayList is the length of this array buffer.
      */
     private Object[] elementData;
     
     /**
      * the size of this ArrayList (the number of elements it contains).
      */
     private int size;
 
     /**
      * construct an empty list with the specified initial capacity.
      *
      * @param initialCapacity the initial capacity of this list
      */
     public MyArrayList( int initialCapacity)
     {
         if (initialCapacity < 0 )
         {
             System.out.println( "Illegal Capacity : " + initialCapacity);
             return ;
         }
         elementData = new Object[initialCapacity];
     }
     
     /**
      * construct an empty list with an initial capacity of ten.
      */
     public MyArrayList()
     {
         this ( 10 );
     }
     
     /**
      * increase the capacity of this ArrayList instance.
      *
      * @param minCapacity the desired minimum capacity
      */
     public void ensureCapacity( int minCapacity)
     {
         int oldCapacity = elementData.length;
         
         if (minCapacity > oldCapacity)
         {
             int newCapacity = 3 * oldCapacity / 2 + 1 ;
             if (minCapacity > newCapacity)
             {
                 newCapacity = minCapacity;
             }
             elementData = Arrays.copyOf(elementData, newCapacity);
         }
     }
     
     /**
      * return the number of elements in the list.
      *
      * @return the number of elements in the list
      */
     public int size() {
         return size;
     }
 
     /**
      * return true if this list contains no element.
      *
      * @return true if this list contains no element
      */
     public boolean isEmpty() {
         return size == 0 ;
     }
 
     /**
      * return true if this list contains the specified element.
      *
      * @param o element whose presence in this list is to be tested
      *
      * @return true if this list contains the specified element
      */
     public boolean contains(Object o) {
         return indexOf(o) >= 0 ;
     }
 
     /**
      * return an array containing all the elements in this list in a proper sequence.
      *
      * @return an array containing all the elements in this list in a proper sequence
      */
     public Object[] toArray() {
         return Arrays.copyOf(elementData, size);
     }
 
     /**
      * appends the specified element to the end of the list.
      *
      * @param element to be appended to this list
      *
      * @return true
      */
     public boolean add(E element) {
         ensureCapacity(size+ 1 );
         elementData[size++] = element;
         return true ;
     }
 
     /**
      * removes the first occurrence of the specified element from this list,
      * if it is present.
      *
      * @param o element to be removed from this list, if present
      *
      * @return true if this list contains the specified element
      */
     public boolean remove(Object o) {
         if (o == null )
         {
             for ( int i= 0 ; i<size; i++)
             {
                 if (elementData[i] == null )
                 {
                     fastRemove(i);
                     return true ;
                 }
             }
         }
         else
         {
             for ( int i= 0 ; i<size; i++)
             {
                 if (o.equals(elementData[i]))
                 {
                     fastRemove(i);
                     return true ;
                 }
             }
         }
         return false ;
     }
     
     /**
      * private remove method that skip bounds checking and
      * does not return the value removed
      * @param index
      */
     private void fastRemove( int index)
     {
         int numMoved = size - index - 1 ;
         if (numMoved > 0 )
         {
             System.arraycopy(elementData, index+ 1 , elementData, index, numMoved);
         }
         elementData[--size] = null ;   // let gc do its work
     }
 
     /**
      * remove all of the elements from this list.
      * the list should be empty after this call returns.
      */
     public void clear() {
         for ( int i= 0 ; i<size; i++)
         {
             elementData[i] = null ;
         }
         size = 0 ;
     }
 
     /**
      * return the element at the specified position in this list.
      *
      * @param index index of the element to return
      *
      * @return the element at the specified position in this list
      */
     public E get( int index) {
         rangeCheck(index);
         return (E)elementData[index];
     }
 
     /**
      * replace the element at the specified position in this list with
      * the specified element.
      *
      * @param index index of the element to replace
      *
      * @param element element to be stored at the specified position
      *
      * @return the element previously at the specified position
      */
     public E set( int index, E element) {
         rangeCheck(index);
         
         E oldValue = (E)elementData[index];
         elementData[index] = element;
         return oldValue;
     }
 
     /**
      * insert the specified element at the specified position in this list.
      *
      * @param index index at which the specified element is to be inserted
      *
      * @param element element to be inserted
      */
     public void add( int index, E element) {
         if (index > size || index < 0 )
         {
             System.out.println( "Index out of bounds, Index : " + index + " , Size : " + size);
             return ;
         }
         
         ensureCapacity(size+ 1 );
         System.arraycopy(elementData, index, elementData, index+ 1 , size-index);
         elementData[index] = element;
         
         size ++;
     }
 
     /**
      * remove the element at the specified position in this list.
      *
      * @param index the index of the element to be removed
      *
      * @return the element previously at the specified position
      */
     public E remove( int index) {
         rangeCheck(index);
         
         E oldValue = (E)elementData[index];
         
         int numMoved = size - index - 1 ;
         if (numMoved > 0 )
         {
             System.arraycopy(elementData, index+ 1 , elementData, index, numMoved);
         }
         elementData[--size] = null ;    // let gc do it work
         
         return oldValue;
     }
 
     /**
      * return the index of the first occurrence of the specified element
      * in this list, or -1 if this list does not contain the element.
      *
      * @param o element to search for
      *
      * @return the index of the first occurrence of the specified element in
      *         this list, or -1 if this list does not contain the element
      */
     public int indexOf(Object o) {
         if (o == null )
         {
             for ( int i= 0 ; i<size; i++)
             {
                 if (elementData[i] == null )
                 {
                     return i;
                 }
             }
         }
         else
         {
             for ( int i= 0 ; i<size; i++)
             {
                 if (o.equals(elementData[i]))
                 {
                     return i;
                 }
             }
         }
         
         return - 1 ;
     }
 
     /**
      * return the index of the last occurrence of the specified element
      * in this list, or -1 if this list does not contain the element.
      *
      * @param o element to search for
      *
      * @return the index of the first occurrence of the specified element in
      *         this list, or -1 if this list does not contain the element
      */
     public int lastIndexOf(Object o) {
         if (o == null )
         {
             for ( int i=size- 1 ; i>= 0 ; i--)
             {
                 if (elementData[i] == null )
                 {
                     return i;
                 }
             }
         }
         else
         {
             for ( int i=size- 1 ; i>= 0 ; i--)
             {
                 if (o.equals(elementData[i]))
                 {
                     return i;
                 }
             }
         }
         
         return - 1 ;
     }
 
     /**
      * check if the given index is in range.
      *
      * @param index
      */
     private void rangeCheck( int index)
     {
         if (index >= size || index < 0 )
         {
             System.out.println( "Index out of bounds, index : " + index);
             return ;
         }
     }
 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值