generated from ZipCodeCore/OldTexasCode2
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGenericList.java
153 lines (127 loc) · 4.14 KB
/
GenericList.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package Fall0811;
import java.util.Iterator;
public class GenericList<E> implements Iterable<E>{
// class constant
private static final int DEFAULT_CAP = 10;
// instance variables
protected E[] container; // the array is NOT the list
private int listSize;
public Iterator<E> iterator(){
return new GenListIterator();
}
// inner class
private class GenListIterator implements Iterator<E>{
private int indexOfNextElement;
private boolean okToRemove;
private GenListIterator(){
indexOfNextElement = 0;
okToRemove = false;
}
public boolean hasNext(){
return indexOfNextElement < size();
}
public E next(){
assert hasNext();
okToRemove = true;
indexOfNextElement++;
return container[indexOfNextElement - 1];
}
public void remove(){
assert okToRemove;
okToRemove = false;
indexOfNextElement--;
GenericList.this.remove(indexOfNextElement);
}
}
public boolean equals(Object obj){
assert this != null;
if(obj == null)
return false;
else if (this == obj)
return true;
else if( this.getClass() != obj.getClass() )
return false;
else{
// obj is a non null GenericList
GenericList list = (GenericList)obj;
if( list.size() != size() )
return false;
for(int i = 0; i < size(); i++)
if( (get(i) == null && list.get(i) != null) || !get(i).equals(list.get(i)) )
return false;
return true;
}
}
// creates an empty IntList
public GenericList(){
this(DEFAULT_CAP);
}
// pre: initialCap >= 0
public GenericList(int initialCap){
assert initialCap >= 0 : "failed precondition";
container = (E[])(new Object[initialCap]);
listSize = 0;
}
public void insertAll(int pos, GenericList<E> otherList){
for(int i = 0; i < otherList.listSize; i++){
this.insert(pos + i, otherList.container[i]);
}
}
// pre: 0 <= pos < size()
public E remove(int pos){
E result = container[pos];
listSize--;
for(int index = pos; index < size(); index++){
container[index] = container[index + 1];
}
container[listSize] = null;
return result;
}
// pre: 0 <= pos <= size()
public void insert(int pos, E element){
assert 0 <= pos && pos <= size();
if( size() == container.length )
resize();
for(int index = size(); index > pos; index--){
assert index > 0;
container[index] = container[index - 1];
}
container[pos] = element;
listSize++;
}
// get size of list
public int size(){
return listSize;
}
// access elements
// pre: 0 <= position < size()
public E get(int position){
assert 0 <= position && position < size();
return container[position];
}
// pre: none
public void add(E element){
insert(size(), element);
}
private void resize() {
E[] temp = (E[])(new Object[container.length * 2 + 1]);
System.arraycopy(container, 0, temp, 0, size());
container = temp;
}
public String toString(){
StringBuffer result = new StringBuffer("[");
final int LIMIT = size() - 1;
for(int i = 0; i < LIMIT; i++){
if( this == this.get(i) )
result.append("this list");
else{
result.append(get(i));
}
result.append(", ");
}
if( size() != 0)
result.append(get(size() - 1));
result.append("]");
return result.toString();
}
}