Skip to content

Commit 62c1a46

Browse files
authored
Merge pull request eugenp#3663 from nguyennamthai/BAEL-1537
Initial commit for List TDD
2 parents cfbe9f5 + 7debccf commit 62c1a46

2 files changed

Lines changed: 511 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)