1
+ public class MyArrayList <E > extends MyAbstractList <E > {
2
+ public static final int INITIAL_CAPACITY = 16 ;
3
+ private E [] data = (E []) new Object [INITIAL_CAPACITY ];
4
+
5
+ /** Create a default list */
6
+ public MyArrayList () {
7
+ }
8
+
9
+ /** Create a list from an array of objects */
10
+ public MyArrayList (E [] objects ) {
11
+ for (int i = 0 ; i < objects .length ; i ++)
12
+ add (objects [i ]);
13
+ }
14
+
15
+ @ Override /** Add a new element at the specified index */
16
+ public void add (int index , E e ) {
17
+ ensureCapacity ();
18
+
19
+ // Move the elements to the right after the speciied index
20
+ for (int i = size - 1 ; i >= index ; i --)
21
+ data [i + 1 ] = data [i ];
22
+
23
+ // Insert new element to data[index]
24
+ data [index ] = e ;
25
+
26
+ // Increase size by 1
27
+ size ++;
28
+ }
29
+
30
+ /** Create a new larger array, double the current size + 1 */
31
+ private void ensureCapacity () {
32
+ if (size >= data .length ) {
33
+ E [] newData = (E [])(new Object [size * 2 + 1 ]);
34
+ System .arraycopy (data , 0 , newData , 0 , size );
35
+ data = newData ;
36
+ }
37
+ }
38
+
39
+ @ Override /** Clear the list */
40
+ public void clear () {
41
+ data = (E [])new Object [INITIAL_CAPACITY ];
42
+ size = 0 ;
43
+ }
44
+
45
+ @ Override /** Return true if this list contains the element */
46
+ public boolean contains (E e ) {
47
+ for (int i = 0 ; i < size ; i ++)
48
+ if (e .equals (data [i ])) return true ;
49
+
50
+ return false ;
51
+ }
52
+
53
+ @ Override /** Return the element at the specified index */
54
+ public E get (int index ) {
55
+ checkIndex (index );
56
+ return data [index ];
57
+ }
58
+
59
+ private void checkIndex (int index ) {
60
+ if (index < 0 || index >= size )
61
+ throw new IndexOutOfBoundsException ("index" + index + " out of bounds" );
62
+ }
63
+
64
+ @ Override /** Return the index of the first matching element
65
+ * in this list. Return -1 if no match. */
66
+ public int indexOf (E e ) {
67
+ for (int i = 0 ; i < size ; i ++)
68
+ if (e .equals (data [i ])) return i ;
69
+
70
+ return -1 ;
71
+ }
72
+
73
+ @ Override /** Return the index of the last matching element
74
+ * in this list. Return -1 if no match. */
75
+ public int lastIndexOf (E e ) {
76
+ for (int i = size - 1 ; i >= 0 ; i --)
77
+ if (e .equals (data [i ])) return i ;
78
+
79
+ return -1 ;
80
+ }
81
+
82
+ @ Override /** Remove the element at the specified position
83
+ * in this list. Shift any subsequent elements to the left.
84
+ * Return the element that was removed from the list. */
85
+ public E remove (int index ) {
86
+ checkIndex (index );
87
+
88
+ E e = data [index ];
89
+
90
+ // Shift data to the left
91
+ for (int j = index ; j < size - 1 ; j ++)
92
+ data [j ] = data [j + 1 ];
93
+
94
+ data [size - 1 ] = null ; // This element is now null
95
+
96
+ // Decrement size
97
+ size --;
98
+
99
+ return e ;
100
+ }
101
+
102
+ @ Override /** Replace the element at the specified position
103
+ * in this list with the specified element. */
104
+ public E set (int index , E e ) {
105
+ checkIndex (index );
106
+ E old = data [index ];
107
+ data [index ] = e ;
108
+ return old ;
109
+ }
110
+
111
+ @ Override
112
+ public String toString () {
113
+ StringBuilder result = new StringBuilder ("[" );
114
+
115
+ for (int i = 0 ; i < size ; i ++) {
116
+ result .append (data [i ]);
117
+ if (i < size - 1 ) result .append (", " );
118
+ }
119
+
120
+ return result .toString () + "]" ;
121
+ }
122
+
123
+ /** Trims the capacity to current size */
124
+ public void trimToSize () {
125
+ if (size != data .length ) {
126
+ E [] newData = (E [])(new Object [size ]);
127
+ System .arraycopy (data , 0 , newData , 0 , size );
128
+ data = newData ;
129
+ } // If size == capacity, no need to trim
130
+ }
131
+
132
+ @ Override /** Override iterator() defined in Iterable */
133
+ public java .util .Iterator <E > iterator () {
134
+ return new ArrayListIterator ();
135
+ }
136
+
137
+ private class ArrayListIterator
138
+ implements java .util .Iterator <E > {
139
+ private int current = 0 ; // Current index
140
+
141
+ @ Override
142
+ public boolean hasNext () {
143
+ return (current < size );
144
+ }
145
+
146
+ @ Override
147
+ public E next () {
148
+ return data [current ++];
149
+ }
150
+
151
+ @ Override
152
+ public void remove () {
153
+ MyArrayList .this .remove (current );
154
+ }
155
+ }
156
+ }
0 commit comments