Skip to content

Commit a67d99a

Browse files
Nam Thai NguyenNam Thai Nguyen
authored andcommitted
Initial commit for List TDD
1 parent 372cba1 commit a67d99a

2 files changed

Lines changed: 516 additions & 0 deletions

File tree

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
package com.baeldung.java.list;
2+
3+
import java.util.Arrays;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
import java.util.ListIterator;
8+
9+
public class CustomList<E> implements List<E> {
10+
private Object[] internal = {};
11+
12+
@Override
13+
public boolean add(E element) {
14+
// the first cycle
15+
// internal = new Object[1];
16+
// internal[0] = element;
17+
// return true;
18+
19+
Object[] temp = new Object[internal.length + 1];
20+
System.arraycopy(internal, 0, temp, 0, internal.length);
21+
temp[internal.length] = element;
22+
internal = temp;
23+
return true;
24+
}
25+
26+
@SuppressWarnings("unchecked")
27+
@Override
28+
public E get(int index) {
29+
return (E) internal[index];
30+
}
31+
32+
@Override
33+
public void add(int index, E element) {
34+
throw new UnsupportedOperationException();
35+
}
36+
37+
@Override
38+
public boolean addAll(Collection<? extends E> collection) {
39+
throw new UnsupportedOperationException();
40+
}
41+
42+
@Override
43+
public boolean addAll(int index, Collection<? extends E> collection) {
44+
throw new UnsupportedOperationException();
45+
}
46+
47+
@Override
48+
public E remove(int index) {
49+
throw new UnsupportedOperationException();
50+
}
51+
52+
@Override
53+
public boolean remove(Object object) {
54+
throw new UnsupportedOperationException();
55+
}
56+
57+
@Override
58+
public boolean removeAll(Collection<?> collection) {
59+
throw new UnsupportedOperationException();
60+
}
61+
62+
@Override
63+
public boolean retainAll(Collection<?> collection) {
64+
throw new UnsupportedOperationException();
65+
}
66+
67+
@Override
68+
public int size() {
69+
return internal.length;
70+
}
71+
72+
@Override
73+
public boolean isEmpty() {
74+
return internal.length == 0;
75+
}
76+
77+
@Override
78+
public boolean contains(Object object) {
79+
// the first cycle
80+
// if (object.equals(internal[0])) {
81+
// return true;
82+
// }
83+
84+
for (Object element : internal) {
85+
if (object.equals(element)) {
86+
return true;
87+
}
88+
}
89+
return false;
90+
}
91+
92+
@Override
93+
public boolean containsAll(Collection<?> collection) {
94+
// the first cycle
95+
// for (Object element : collection) {
96+
// if (element.equals(internal[0])) {
97+
// return true;
98+
// }
99+
// }
100+
// return false;
101+
102+
for (Object element : collection)
103+
if (!contains(element)) {
104+
return false;
105+
}
106+
return true;
107+
}
108+
109+
@SuppressWarnings("unchecked")
110+
@Override
111+
public E set(int index, E element) {
112+
E oldElement = (E) internal[index];
113+
internal[index] = element;
114+
return oldElement;
115+
}
116+
117+
@Override
118+
public void clear() {
119+
internal = new Object[0];
120+
}
121+
122+
@Override
123+
public int indexOf(Object object) {
124+
// the first cycle
125+
// if (object.equals(internal[0])) {
126+
// return 0;
127+
// }
128+
// return -1;
129+
130+
for (int i = 0; i < internal.length; i++) {
131+
if (object.equals(internal[i])) {
132+
return i;
133+
}
134+
}
135+
return -1;
136+
}
137+
138+
@Override
139+
public int lastIndexOf(Object object) {
140+
// the first cycle
141+
// if (object.equals(internal[0])) {
142+
// return 0;
143+
// }
144+
// return -1;
145+
146+
for (int i = internal.length - 1; i >= 0; i--) {
147+
if (object.equals(internal[i])) {
148+
return i;
149+
}
150+
}
151+
return -1;
152+
}
153+
154+
@SuppressWarnings("unchecked")
155+
@Override
156+
public List<E> subList(int fromIndex, int toIndex) {
157+
// the first cycle
158+
// return (List<E>) Arrays.asList(internal);
159+
160+
Object[] temp = new Object[toIndex - fromIndex];
161+
System.arraycopy(internal, fromIndex, temp, 0, temp.length);
162+
return (List<E>) Arrays.asList(temp);
163+
}
164+
165+
@Override
166+
public Object[] toArray() {
167+
return Arrays.copyOf(internal, internal.length);
168+
}
169+
170+
@SuppressWarnings("unchecked")
171+
@Override
172+
public <T> T[] toArray(T[] array) {
173+
// the first cycle
174+
// for (int i = 0; i < array.length; i++) {
175+
// array[i] = (T) internal[i];
176+
// }
177+
// return array;
178+
179+
// the second cycle
180+
// if (array.length < internal.length) {
181+
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
182+
// }
183+
// return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
184+
185+
if (array.length < internal.length) {
186+
return (T[]) Arrays.copyOf(internal, internal.length, array.getClass());
187+
}
188+
System.arraycopy(internal, 0, array, 0, internal.length);
189+
if (array.length > internal.length) {
190+
array[internal.length] = null;
191+
}
192+
return array;
193+
}
194+
195+
@Override
196+
public Iterator<E> iterator() {
197+
return new CustomIterator();
198+
}
199+
200+
@Override
201+
public ListIterator<E> listIterator() {
202+
return null;
203+
}
204+
205+
@Override
206+
public ListIterator<E> listIterator(int index) {
207+
return null;
208+
}
209+
210+
private class CustomIterator implements Iterator<E> {
211+
int index;
212+
213+
@Override
214+
public boolean hasNext() {
215+
// the first cycle
216+
// return true;
217+
218+
return index != internal.length;
219+
}
220+
221+
@SuppressWarnings("unchecked")
222+
@Override
223+
public E next() {
224+
// the first cycle
225+
// return (E) CustomList.this.internal[0];
226+
227+
E element = (E) CustomList.this.internal[index];
228+
index++;
229+
return element;
230+
}
231+
}
232+
}

0 commit comments

Comments
 (0)