11package by .andd3dfx .collections .custom ;
22
33import lombok .RequiredArgsConstructor ;
4+ import org .apache .commons .lang3 .NotImplementedException ;
45
56import java .util .Arrays ;
67import java .util .Collection ;
78import java .util .Iterator ;
9+ import java .util .List ;
10+ import java .util .ListIterator ;
811
912/**
1013 * @see <a href="https://youtu.be/u7Vyh567ljs">Video solution part1</a>, <a href="https://youtu.be/VBdYvDW8WL8">part2</a>
1114 */
12- public class CustomArrayList <T > implements Collection <T > {
15+ public class CustomArrayList <T > implements List <T > {
1316
1417 private static final int DEFAULT_INITIAL_SIZE = 10 ;
1518 private static final float RESIZE_FACTOR = 1.75f ;
@@ -26,27 +29,33 @@ public CustomArrayList(int length) {
2629 array = (T []) new Object [length ];
2730 }
2831
32+ @ Override
2933 public int size () {
3034 return size ;
3135 }
3236
37+ @ Override
3338 public boolean isEmpty () {
3439 return size == 0 ;
3540 }
3641
37- public void set (int index , T value ) {
42+ @ Override
43+ public T set (int index , T value ) {
3844 if (index < 0 || index >= size ) {
3945 throw new IndexOutOfBoundsException (String .format ("Wrong index: %d" , index ));
4046 }
4147
4248 array [index ] = value ;
49+ return value ;
4350 }
4451
52+ @ Override
4553 public boolean add (T value ) {
4654 add (size , value );
4755 return true ;
4856 }
4957
58+ @ Override
5059 public void add (int index , T value ) {
5160 if (index < 0 || index > size ) {
5261 throw new IndexOutOfBoundsException ();
@@ -69,6 +78,7 @@ public void add(int index, T value) {
6978 size ++;
7079 }
7180
81+ @ Override
7282 public T get (int index ) {
7383 if (index < 0 || index >= size ) {
7484 throw new IndexOutOfBoundsException (String .format ("Wrong index: %d" , index ));
@@ -77,6 +87,7 @@ public T get(int index) {
7787 return array [index ];
7888 }
7989
90+ @ Override
8091 public T remove (int index ) {
8192 if (index < 0 || index >= size ) {
8293 throw new IndexOutOfBoundsException (String .format ("Wrong index: %d" , index ));
@@ -99,6 +110,60 @@ public T remove(int index) {
99110 return result ;
100111 }
101112
113+ @ Override
114+ public int indexOf (Object o ) {
115+ if (o == null ) {
116+ for (int i = 0 ; i < size ; i ++) {
117+ if (array [i ] == null ) {
118+ return i ;
119+ }
120+ }
121+ } else {
122+ for (int i = 0 ; i < size ; i ++) {
123+ if (o .equals (array [i ])) {
124+ return i ;
125+ }
126+ }
127+ }
128+ return -1 ;
129+ }
130+
131+ @ Override
132+ public int lastIndexOf (Object o ) {
133+ if (o == null ) {
134+ for (int i = size - 1 ; i >= 0 ; i --) {
135+ if (array [i ] == null ) {
136+ return i ;
137+ }
138+ }
139+ } else {
140+ for (int i = size - 1 ; i >= 0 ; i --) {
141+ if (o .equals (array [i ])) {
142+ return i ;
143+ }
144+ }
145+ }
146+ return -1 ;
147+ }
148+
149+ @ Override
150+ public ListIterator <T > listIterator () {
151+ // TODO add implementation
152+ throw new NotImplementedException ();
153+ }
154+
155+ @ Override
156+ public ListIterator <T > listIterator (int index ) {
157+ // TODO add implementation
158+ throw new NotImplementedException ();
159+ }
160+
161+ @ Override
162+ public List <T > subList (int fromIndex , int toIndex ) {
163+ // TODO add implementation
164+ throw new NotImplementedException ();
165+ }
166+
102167 public boolean remove (Object value ) {
103168 var i = 0 ;
104169 while (i < size ) {
@@ -155,6 +220,12 @@ public boolean addAll(Collection<? extends T> c) {
155220 return true ;
156221 }
157222
223+ @ Override
224+ public boolean addAll (int index , Collection <? extends T > c ) {
225+ // TODO add implementation
226+ throw new NotImplementedException ();
227+ }
228+
158229 @ Override
159230 public boolean removeAll (Collection <?> c ) {
160231 var result = false ;
@@ -189,6 +260,7 @@ private boolean checkEquality(Object value1, Object value2) {
189260 return value1 .equals (value2 );
190261 }
191262
263+ @ Override
192264 public void clear () {
193265 array = (T []) new Object [DEFAULT_INITIAL_SIZE ];
194266 size = 0 ;
@@ -224,6 +296,7 @@ public <T1> T1[] toArray(T1[] a) {
224296
225297 @ RequiredArgsConstructor
226298 public class CustomIterator <E > implements Iterator <E > {
299+
227300 private final E [] array ;
228301 private final int size ;
229302
0 commit comments