-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImList.java
70 lines (56 loc) · 1.52 KB
/
ImList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
* From
* @author cs2030
* An immutable implementation of {@code ArrayList}.
*/
public class ImList<E> implements Iterable<E> {
private final ArrayList<E> elems;
public ImList() {
this.elems = new ArrayList<E>();
}
public ImList(List<? extends E> list) {
this.elems = new ArrayList<E>(list);
}
public ImList<E> add(E elem) {
ImList<E> newList = new ImList<E>(this.elems);
newList.elems.add(elem);
return newList;
}
public ImList<E> addAll(List<? extends E> list) {
ImList<E> newList = new ImList<E>(this.elems);
newList.elems.addAll(list);
return newList;
}
public E get(int index) {
return this.elems.get(index);
}
public int indexOf(Object obj) {
return this.elems.indexOf(obj);
}
public boolean isEmpty() {
return this.elems.isEmpty();
}
public Iterator<E> iterator() {
return this.elems.iterator();
}
public ImList<E> remove(int index) {
ImList<E> newList = new ImList<E>(this.elems);
newList.elems.remove(index);
return newList;
}
public ImList<E> set(int index, E elem) {
ImList<E> newList = new ImList<E>(this.elems);
newList.elems.set(index, elem);
return newList;
}
public int size() {
return this.elems.size();
}
@Override
public String toString() {
return this.elems.toString();
}
}