-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList.java
226 lines (190 loc) · 5.82 KB
/
ArrayList.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.util.Iterator;
public class ArrayList<T> implements ListADT<T>, Iterable<T> {
private final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
protected int rear;
protected T[] list;
/**
* Creates an empty list using the default capacity.
*/
public ArrayList() {
rear = 0;
list = (T[])(new Object[DEFAULT_CAPACITY]);
}
/**
* Creates an empty list using the specified capacity.
*
* @param initialCapacity the integer value of the size of the array list
*/
public ArrayList (int initialCapacity) {
rear = 0;
list = (T[])(new Object[initialCapacity]);
}
/**
* Removes and returns the last element in this list.
*
* @return the last element in the list
* @throws EmptyCollectionException if an empty collection exception occurs
*/
public T removeLast () throws EmptyCollectionException {
if (rear == 0) throw new EmptyCollectionException("list");
rear-- ;
T result = list[rear];
list[rear] = null;
return result;
}
/**
* Removes and returns the first element in this list.
*
* @return the first element in the list
* @throws EmptyCollectionException if an empty collection exception occurs
*/
public T removeFirst() throws EmptyCollectionException {
if (rear == 0) throw new EmptyCollectionException("list");
rear--;
T result = list[0];
for (int i = 1 ; i <= rear; i++) {
list[i-1] = list[i];
}
list[rear] = null;
return result;
}
/**
* Removes and returns the specified element.
*
* @param element the element to be removed and returned
* from the list
* @return the removed element
* @throws ElementNotFoundException if an element not found exception occurs
*/
public T remove (T element) {
T result;
int index = find (element);
if (index == NOT_FOUND)
throw new ElementNotFoundException ("list");
result = list[index];
rear--;
for (int i = index; i < rear; i++) {
list[i] = list[i+1];
}
list[rear] = null;
return result;
}
/**
* Returns a reference to the element at the front of this list.
* The element is not removed from the list. Throws an
* EmptyCollectionException if the list is empty.
*
* @return a reference to the first element in the
* list
* @throws EmptyCollectionException if an empty collection exception occurs
*/
public T first() throws EmptyCollectionException {
if (rear == 0) throw new EmptyCollectionException("list");
return list[0] ;
}
/**
* Returns a reference to the element at the rear of this list.
* The element is not removed from the list. Throws an
* EmptyCollectionException if the list is empty.
*
* @return a reference to the last element of this list
* @throws EmptyCollectionException if an empty collection exception occurs
*/
public T last() throws EmptyCollectionException {
if (rear == 0) throw new EmptyCollectionException("list");
return list[rear-1];
}
/**
* Returns true if this list contains the specified element.
*
* @param target the element that the list is searched for
* @return true if the target is in the list, false if otherwise
*/
public boolean contains (T target) {
return (find(target) != NOT_FOUND);
}
/**
* Returns the array index of the specified element, or the
* constant NOT_FOUND if it is not found.
*
* @param target the element that the list will be searched for
* @return the integer index into the array containing the target
* element, or the NOT_FOUND constant
*/
public int find (T target) {
int scan = 0, result = NOT_FOUND;
boolean found = false;
if (! isEmpty())
while (! found && scan < rear)
if (target.equals(list[scan]))
found = true;
else
scan++;
if (found)
result = scan;
return result;
}
/**
* Returns the element at index i in the list
*
* @param i index in the list
* @return the element at i
*/
public T getElement(int i) throws ElementNotFoundException {
if (i < size() && i > NOT_FOUND) {
return list[i] ;
}
else {
throw new ElementNotFoundException("list");
}
}
/**
* Returns true if this list is empty and false otherwise.
*
* @return true if the list is empty and false if otherwise
*/
public boolean isEmpty() {
return (rear == 0);
}
/**
* Returns the number of elements currently in this list.
*
* @return the integer representation of the number of elements in the list
*/
public int size() {
return rear ;
}
/**
* Returns an iterator for the elements currently in this list.
*
* @return an iterator for the elements in this list
*/
public Iterator<T> iterator()
{
return new ArrayIterator<T> (list,rear);
}
/**
* Returns a string representation of this list.
*
* @return the string representation of this list
*/
public String toString() {
String s = "" ;
for (int i = 0; i < rear; i++) {
s += list[i].toString();
}
return s;
}
/**
* Creates a new array to store the contents of this list with
* twice the capacity of the old one.
*/
protected void expandCapacity()
{
T[] larger = (T[])(new Object[list.length*2]);
for (int scan = 0; scan < list.length; scan++)
larger[scan] = list[scan];
list = larger;
}
}