@@ -10,74 +10,94 @@ public class CustomList<E> implements List<E> {
1010 private Object [] internal = {};
1111
1212 @ Override
13- public void add (int index , E element ) {
14- throw new UnsupportedOperationException ();
13+ public boolean isEmpty () {
14+ // the first cycle
15+ // return true;
16+
17+ // the second cycle
18+ // if (internal.length != 0) {
19+ // return false;
20+ // } else {
21+ // return true;
22+ // }
23+
24+ // refactoring
25+ return internal .length == 0 ;
1526 }
1627
1728 @ Override
18- public boolean addAll (Collection <? extends E > collection ) {
19- throw new UnsupportedOperationException ();
29+ public int size () {
30+ // the first cycle
31+ // if (isEmpty()) {
32+ // return 0;
33+ // } else {
34+ // return internal.length;
35+ // }
36+
37+ // refactoring
38+ return internal .length ;
2039 }
2140
41+ @ SuppressWarnings ("unchecked" )
2242 @ Override
23- public boolean addAll (int index , Collection <? extends E > collection ) {
24- throw new UnsupportedOperationException ();
43+ public E get (int index ) {
44+ // the first cycle
45+ // return (E) internal[0];
46+
47+ // improvement
48+ return (E ) internal [index ];
2549 }
2650
2751 @ Override
28- public E remove (int index ) {
29- throw new UnsupportedOperationException ();
52+ public boolean add (E element ) {
53+ // the first cycle
54+ // internal = new Object[] { element };
55+ // return true;
56+
57+ // the second cycle
58+ Object [] temp = Arrays .copyOf (internal , internal .length + 1 );
59+ temp [internal .length ] = element ;
60+ internal = temp ;
61+ return true ;
3062 }
3163
3264 @ Override
33- public boolean remove ( Object object ) {
65+ public void add ( int index , E element ) {
3466 throw new UnsupportedOperationException ();
3567 }
3668
3769 @ Override
38- public boolean removeAll (Collection <?> collection ) {
70+ public boolean addAll (Collection <? extends E > collection ) {
3971 throw new UnsupportedOperationException ();
4072 }
4173
4274 @ Override
43- public boolean retainAll ( Collection <?> collection ) {
75+ public boolean addAll ( int index , Collection <? extends E > collection ) {
4476 throw new UnsupportedOperationException ();
4577 }
4678
4779 @ Override
48- public int size ( ) {
49- return internal . length ;
80+ public E remove ( int index ) {
81+ throw new UnsupportedOperationException () ;
5082 }
5183
5284 @ Override
53- public boolean isEmpty ( ) {
54- return internal . length == 0 ;
85+ public boolean remove ( Object object ) {
86+ throw new UnsupportedOperationException () ;
5587 }
5688
5789 @ Override
58- public boolean add (E element ) {
59- // the first cycle
60- // internal = new Object[1];
61- // internal[0] = element;
62- // return true;
63-
64- Object [] temp = new Object [internal .length + 1 ];
65- System .arraycopy (internal , 0 , temp , 0 , internal .length );
66- temp [internal .length ] = element ;
67- internal = temp ;
68- return true ;
90+ public boolean removeAll (Collection <?> collection ) {
91+ throw new UnsupportedOperationException ();
6992 }
7093
71- @ SuppressWarnings ("unchecked" )
7294 @ Override
73- public E get ( int index ) {
74- return ( E ) internal [ index ] ;
95+ public boolean retainAll ( Collection <?> collection ) {
96+ throw new UnsupportedOperationException () ;
7597 }
7698
7799 @ Override
78100 public boolean contains (Object object ) {
79- // return false
80-
81101 for (Object element : internal ) {
82102 if (object .equals (element )) {
83103 return true ;
@@ -88,14 +108,6 @@ public boolean contains(Object object) {
88108
89109 @ Override
90110 public boolean containsAll (Collection <?> collection ) {
91- // the first cycle
92- // for (Object element : collection) {
93- // if (element.equals(internal[0])) {
94- // return true;
95- // }
96- // }
97- // return false;
98-
99111 for (Object element : collection )
100112 if (!contains (element )) {
101113 return false ;
@@ -118,12 +130,6 @@ public void clear() {
118130
119131 @ Override
120132 public int indexOf (Object object ) {
121- // the first cycle
122- // if (object.equals(internal[0])) {
123- // return 0;
124- // }
125- // return -1;
126-
127133 for (int i = 0 ; i < internal .length ; i ++) {
128134 if (object .equals (internal [i ])) {
129135 return i ;
@@ -134,12 +140,6 @@ public int indexOf(Object object) {
134140
135141 @ Override
136142 public int lastIndexOf (Object object ) {
137- // the first cycle
138- // if (object.equals(internal[0])) {
139- // return 0;
140- // }
141- // return -1;
142-
143143 for (int i = internal .length - 1 ; i >= 0 ; i --) {
144144 if (object .equals (internal [i ])) {
145145 return i ;
@@ -151,9 +151,6 @@ public int lastIndexOf(Object object) {
151151 @ SuppressWarnings ("unchecked" )
152152 @ Override
153153 public List <E > subList (int fromIndex , int toIndex ) {
154- // the first cycle
155- // return (List<E>) Arrays.asList(internal);
156-
157154 Object [] temp = new Object [toIndex - fromIndex ];
158155 System .arraycopy (internal , fromIndex , temp , 0 , temp .length );
159156 return (List <E >) Arrays .asList (temp );
@@ -167,16 +164,6 @@ public Object[] toArray() {
167164 @ SuppressWarnings ("unchecked" )
168165 @ Override
169166 public <T > T [] toArray (T [] array ) {
170- // the first cycle
171- // array[0] = (T) internal[0];
172- // return array;
173-
174- // the second cycle
175- // if (array.length < internal.length) {
176- // return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
177- // }
178- // return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
179-
180167 if (array .length < internal .length ) {
181168 return (T []) Arrays .copyOf (internal , internal .length , array .getClass ());
182169 }
@@ -209,18 +196,12 @@ private class CustomIterator implements Iterator<E> {
209196
210197 @ Override
211198 public boolean hasNext () {
212- // the first cycle
213- // return true;
214-
215199 return index != internal .length ;
216200 }
217201
218202 @ SuppressWarnings ("unchecked" )
219203 @ Override
220204 public E next () {
221- // the first cycle
222- // return (E) CustomList.this.internal[0];
223-
224205 E element = (E ) CustomList .this .internal [index ];
225206 index ++;
226207 return element ;
0 commit comments