jaroslav@597
|
1 |
/*
|
jaroslav@597
|
2 |
* Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@597
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@597
|
4 |
*
|
jaroslav@597
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@597
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@597
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@597
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@597
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@597
|
10 |
*
|
jaroslav@597
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@597
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@597
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@597
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@597
|
15 |
* accompanied this code).
|
jaroslav@597
|
16 |
*
|
jaroslav@597
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@597
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@597
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@597
|
20 |
*
|
jaroslav@597
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@597
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@597
|
23 |
* questions.
|
jaroslav@597
|
24 |
*/
|
jaroslav@597
|
25 |
|
jaroslav@597
|
26 |
package java.util;
|
jaroslav@597
|
27 |
|
jaroslav@599
|
28 |
|
jaroslav@597
|
29 |
/**
|
jaroslav@597
|
30 |
* The {@code Vector} class implements a growable array of
|
jaroslav@597
|
31 |
* objects. Like an array, it contains components that can be
|
jaroslav@597
|
32 |
* accessed using an integer index. However, the size of a
|
jaroslav@597
|
33 |
* {@code Vector} can grow or shrink as needed to accommodate
|
jaroslav@597
|
34 |
* adding and removing items after the {@code Vector} has been created.
|
jaroslav@597
|
35 |
*
|
jaroslav@597
|
36 |
* <p>Each vector tries to optimize storage management by maintaining a
|
jaroslav@597
|
37 |
* {@code capacity} and a {@code capacityIncrement}. The
|
jaroslav@597
|
38 |
* {@code capacity} is always at least as large as the vector
|
jaroslav@597
|
39 |
* size; it is usually larger because as components are added to the
|
jaroslav@597
|
40 |
* vector, the vector's storage increases in chunks the size of
|
jaroslav@597
|
41 |
* {@code capacityIncrement}. An application can increase the
|
jaroslav@597
|
42 |
* capacity of a vector before inserting a large number of
|
jaroslav@597
|
43 |
* components; this reduces the amount of incremental reallocation.
|
jaroslav@597
|
44 |
*
|
jaroslav@597
|
45 |
* <p><a name="fail-fast"/>
|
jaroslav@597
|
46 |
* The iterators returned by this class's {@link #iterator() iterator} and
|
jaroslav@597
|
47 |
* {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:
|
jaroslav@597
|
48 |
* if the vector is structurally modified at any time after the iterator is
|
jaroslav@597
|
49 |
* created, in any way except through the iterator's own
|
jaroslav@597
|
50 |
* {@link ListIterator#remove() remove} or
|
jaroslav@597
|
51 |
* {@link ListIterator#add(Object) add} methods, the iterator will throw a
|
jaroslav@597
|
52 |
* {@link ConcurrentModificationException}. Thus, in the face of
|
jaroslav@597
|
53 |
* concurrent modification, the iterator fails quickly and cleanly, rather
|
jaroslav@597
|
54 |
* than risking arbitrary, non-deterministic behavior at an undetermined
|
jaroslav@597
|
55 |
* time in the future. The {@link Enumeration Enumerations} returned by
|
jaroslav@597
|
56 |
* the {@link #elements() elements} method are <em>not</em> fail-fast.
|
jaroslav@597
|
57 |
*
|
jaroslav@597
|
58 |
* <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
|
jaroslav@597
|
59 |
* as it is, generally speaking, impossible to make any hard guarantees in the
|
jaroslav@597
|
60 |
* presence of unsynchronized concurrent modification. Fail-fast iterators
|
jaroslav@597
|
61 |
* throw {@code ConcurrentModificationException} on a best-effort basis.
|
jaroslav@597
|
62 |
* Therefore, it would be wrong to write a program that depended on this
|
jaroslav@597
|
63 |
* exception for its correctness: <i>the fail-fast behavior of iterators
|
jaroslav@597
|
64 |
* should be used only to detect bugs.</i>
|
jaroslav@597
|
65 |
*
|
jaroslav@597
|
66 |
* <p>As of the Java 2 platform v1.2, this class was retrofitted to
|
jaroslav@597
|
67 |
* implement the {@link List} interface, making it a member of the
|
jaroslav@597
|
68 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
|
jaroslav@597
|
69 |
* Java Collections Framework</a>. Unlike the new collection
|
jaroslav@597
|
70 |
* implementations, {@code Vector} is synchronized. If a thread-safe
|
jaroslav@597
|
71 |
* implementation is not needed, it is recommended to use {@link
|
jaroslav@597
|
72 |
* ArrayList} in place of {@code Vector}.
|
jaroslav@597
|
73 |
*
|
jaroslav@597
|
74 |
* @author Lee Boynton
|
jaroslav@597
|
75 |
* @author Jonathan Payne
|
jaroslav@597
|
76 |
* @see Collection
|
jaroslav@597
|
77 |
* @see LinkedList
|
jaroslav@597
|
78 |
* @since JDK1.0
|
jaroslav@597
|
79 |
*/
|
jaroslav@597
|
80 |
public class Vector<E>
|
jaroslav@597
|
81 |
extends AbstractList<E>
|
jaroslav@597
|
82 |
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
|
jaroslav@597
|
83 |
{
|
jaroslav@597
|
84 |
/**
|
jaroslav@597
|
85 |
* The array buffer into which the components of the vector are
|
jaroslav@597
|
86 |
* stored. The capacity of the vector is the length of this array buffer,
|
jaroslav@597
|
87 |
* and is at least large enough to contain all the vector's elements.
|
jaroslav@597
|
88 |
*
|
jaroslav@597
|
89 |
* <p>Any array elements following the last element in the Vector are null.
|
jaroslav@597
|
90 |
*
|
jaroslav@597
|
91 |
* @serial
|
jaroslav@597
|
92 |
*/
|
jaroslav@597
|
93 |
protected Object[] elementData;
|
jaroslav@597
|
94 |
|
jaroslav@597
|
95 |
/**
|
jaroslav@597
|
96 |
* The number of valid components in this {@code Vector} object.
|
jaroslav@597
|
97 |
* Components {@code elementData[0]} through
|
jaroslav@597
|
98 |
* {@code elementData[elementCount-1]} are the actual items.
|
jaroslav@597
|
99 |
*
|
jaroslav@597
|
100 |
* @serial
|
jaroslav@597
|
101 |
*/
|
jaroslav@597
|
102 |
protected int elementCount;
|
jaroslav@597
|
103 |
|
jaroslav@597
|
104 |
/**
|
jaroslav@597
|
105 |
* The amount by which the capacity of the vector is automatically
|
jaroslav@597
|
106 |
* incremented when its size becomes greater than its capacity. If
|
jaroslav@597
|
107 |
* the capacity increment is less than or equal to zero, the capacity
|
jaroslav@597
|
108 |
* of the vector is doubled each time it needs to grow.
|
jaroslav@597
|
109 |
*
|
jaroslav@597
|
110 |
* @serial
|
jaroslav@597
|
111 |
*/
|
jaroslav@597
|
112 |
protected int capacityIncrement;
|
jaroslav@597
|
113 |
|
jaroslav@597
|
114 |
/** use serialVersionUID from JDK 1.0.2 for interoperability */
|
jaroslav@597
|
115 |
private static final long serialVersionUID = -2767605614048989439L;
|
jaroslav@597
|
116 |
|
jaroslav@597
|
117 |
/**
|
jaroslav@597
|
118 |
* Constructs an empty vector with the specified initial capacity and
|
jaroslav@597
|
119 |
* capacity increment.
|
jaroslav@597
|
120 |
*
|
jaroslav@597
|
121 |
* @param initialCapacity the initial capacity of the vector
|
jaroslav@597
|
122 |
* @param capacityIncrement the amount by which the capacity is
|
jaroslav@597
|
123 |
* increased when the vector overflows
|
jaroslav@597
|
124 |
* @throws IllegalArgumentException if the specified initial capacity
|
jaroslav@597
|
125 |
* is negative
|
jaroslav@597
|
126 |
*/
|
jaroslav@597
|
127 |
public Vector(int initialCapacity, int capacityIncrement) {
|
jaroslav@597
|
128 |
super();
|
jaroslav@597
|
129 |
if (initialCapacity < 0)
|
jaroslav@597
|
130 |
throw new IllegalArgumentException("Illegal Capacity: "+
|
jaroslav@597
|
131 |
initialCapacity);
|
jaroslav@597
|
132 |
this.elementData = new Object[initialCapacity];
|
jaroslav@597
|
133 |
this.capacityIncrement = capacityIncrement;
|
jaroslav@597
|
134 |
}
|
jaroslav@597
|
135 |
|
jaroslav@597
|
136 |
/**
|
jaroslav@597
|
137 |
* Constructs an empty vector with the specified initial capacity and
|
jaroslav@597
|
138 |
* with its capacity increment equal to zero.
|
jaroslav@597
|
139 |
*
|
jaroslav@597
|
140 |
* @param initialCapacity the initial capacity of the vector
|
jaroslav@597
|
141 |
* @throws IllegalArgumentException if the specified initial capacity
|
jaroslav@597
|
142 |
* is negative
|
jaroslav@597
|
143 |
*/
|
jaroslav@597
|
144 |
public Vector(int initialCapacity) {
|
jaroslav@597
|
145 |
this(initialCapacity, 0);
|
jaroslav@597
|
146 |
}
|
jaroslav@597
|
147 |
|
jaroslav@597
|
148 |
/**
|
jaroslav@597
|
149 |
* Constructs an empty vector so that its internal data array
|
jaroslav@597
|
150 |
* has size {@code 10} and its standard capacity increment is
|
jaroslav@597
|
151 |
* zero.
|
jaroslav@597
|
152 |
*/
|
jaroslav@597
|
153 |
public Vector() {
|
jaroslav@597
|
154 |
this(10);
|
jaroslav@597
|
155 |
}
|
jaroslav@597
|
156 |
|
jaroslav@597
|
157 |
/**
|
jaroslav@597
|
158 |
* Constructs a vector containing the elements of the specified
|
jaroslav@597
|
159 |
* collection, in the order they are returned by the collection's
|
jaroslav@597
|
160 |
* iterator.
|
jaroslav@597
|
161 |
*
|
jaroslav@597
|
162 |
* @param c the collection whose elements are to be placed into this
|
jaroslav@597
|
163 |
* vector
|
jaroslav@597
|
164 |
* @throws NullPointerException if the specified collection is null
|
jaroslav@597
|
165 |
* @since 1.2
|
jaroslav@597
|
166 |
*/
|
jaroslav@597
|
167 |
public Vector(Collection<? extends E> c) {
|
jaroslav@597
|
168 |
elementData = c.toArray();
|
jaroslav@597
|
169 |
elementCount = elementData.length;
|
jaroslav@597
|
170 |
// c.toArray might (incorrectly) not return Object[] (see 6260652)
|
jaroslav@597
|
171 |
if (elementData.getClass() != Object[].class)
|
jaroslav@597
|
172 |
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
|
jaroslav@597
|
173 |
}
|
jaroslav@597
|
174 |
|
jaroslav@597
|
175 |
/**
|
jaroslav@597
|
176 |
* Copies the components of this vector into the specified array.
|
jaroslav@597
|
177 |
* The item at index {@code k} in this vector is copied into
|
jaroslav@597
|
178 |
* component {@code k} of {@code anArray}.
|
jaroslav@597
|
179 |
*
|
jaroslav@597
|
180 |
* @param anArray the array into which the components get copied
|
jaroslav@597
|
181 |
* @throws NullPointerException if the given array is null
|
jaroslav@597
|
182 |
* @throws IndexOutOfBoundsException if the specified array is not
|
jaroslav@597
|
183 |
* large enough to hold all the components of this vector
|
jaroslav@597
|
184 |
* @throws ArrayStoreException if a component of this vector is not of
|
jaroslav@597
|
185 |
* a runtime type that can be stored in the specified array
|
jaroslav@597
|
186 |
* @see #toArray(Object[])
|
jaroslav@597
|
187 |
*/
|
jaroslav@597
|
188 |
public synchronized void copyInto(Object[] anArray) {
|
jaroslav@597
|
189 |
System.arraycopy(elementData, 0, anArray, 0, elementCount);
|
jaroslav@597
|
190 |
}
|
jaroslav@597
|
191 |
|
jaroslav@597
|
192 |
/**
|
jaroslav@597
|
193 |
* Trims the capacity of this vector to be the vector's current
|
jaroslav@597
|
194 |
* size. If the capacity of this vector is larger than its current
|
jaroslav@597
|
195 |
* size, then the capacity is changed to equal the size by replacing
|
jaroslav@597
|
196 |
* its internal data array, kept in the field {@code elementData},
|
jaroslav@597
|
197 |
* with a smaller one. An application can use this operation to
|
jaroslav@597
|
198 |
* minimize the storage of a vector.
|
jaroslav@597
|
199 |
*/
|
jaroslav@597
|
200 |
public synchronized void trimToSize() {
|
jaroslav@597
|
201 |
modCount++;
|
jaroslav@597
|
202 |
int oldCapacity = elementData.length;
|
jaroslav@597
|
203 |
if (elementCount < oldCapacity) {
|
jaroslav@597
|
204 |
elementData = Arrays.copyOf(elementData, elementCount);
|
jaroslav@597
|
205 |
}
|
jaroslav@597
|
206 |
}
|
jaroslav@597
|
207 |
|
jaroslav@597
|
208 |
/**
|
jaroslav@597
|
209 |
* Increases the capacity of this vector, if necessary, to ensure
|
jaroslav@597
|
210 |
* that it can hold at least the number of components specified by
|
jaroslav@597
|
211 |
* the minimum capacity argument.
|
jaroslav@597
|
212 |
*
|
jaroslav@597
|
213 |
* <p>If the current capacity of this vector is less than
|
jaroslav@597
|
214 |
* {@code minCapacity}, then its capacity is increased by replacing its
|
jaroslav@597
|
215 |
* internal data array, kept in the field {@code elementData}, with a
|
jaroslav@597
|
216 |
* larger one. The size of the new data array will be the old size plus
|
jaroslav@597
|
217 |
* {@code capacityIncrement}, unless the value of
|
jaroslav@597
|
218 |
* {@code capacityIncrement} is less than or equal to zero, in which case
|
jaroslav@597
|
219 |
* the new capacity will be twice the old capacity; but if this new size
|
jaroslav@597
|
220 |
* is still smaller than {@code minCapacity}, then the new capacity will
|
jaroslav@597
|
221 |
* be {@code minCapacity}.
|
jaroslav@597
|
222 |
*
|
jaroslav@597
|
223 |
* @param minCapacity the desired minimum capacity
|
jaroslav@597
|
224 |
*/
|
jaroslav@597
|
225 |
public synchronized void ensureCapacity(int minCapacity) {
|
jaroslav@597
|
226 |
if (minCapacity > 0) {
|
jaroslav@597
|
227 |
modCount++;
|
jaroslav@597
|
228 |
ensureCapacityHelper(minCapacity);
|
jaroslav@597
|
229 |
}
|
jaroslav@597
|
230 |
}
|
jaroslav@597
|
231 |
|
jaroslav@597
|
232 |
/**
|
jaroslav@597
|
233 |
* This implements the unsynchronized semantics of ensureCapacity.
|
jaroslav@597
|
234 |
* Synchronized methods in this class can internally call this
|
jaroslav@597
|
235 |
* method for ensuring capacity without incurring the cost of an
|
jaroslav@597
|
236 |
* extra synchronization.
|
jaroslav@597
|
237 |
*
|
jaroslav@597
|
238 |
* @see #ensureCapacity(int)
|
jaroslav@597
|
239 |
*/
|
jaroslav@597
|
240 |
private void ensureCapacityHelper(int minCapacity) {
|
jaroslav@597
|
241 |
// overflow-conscious code
|
jaroslav@597
|
242 |
if (minCapacity - elementData.length > 0)
|
jaroslav@597
|
243 |
grow(minCapacity);
|
jaroslav@597
|
244 |
}
|
jaroslav@597
|
245 |
|
jaroslav@597
|
246 |
/**
|
jaroslav@597
|
247 |
* The maximum size of array to allocate.
|
jaroslav@597
|
248 |
* Some VMs reserve some header words in an array.
|
jaroslav@597
|
249 |
* Attempts to allocate larger arrays may result in
|
jaroslav@597
|
250 |
* OutOfMemoryError: Requested array size exceeds VM limit
|
jaroslav@597
|
251 |
*/
|
jaroslav@597
|
252 |
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
|
jaroslav@597
|
253 |
|
jaroslav@597
|
254 |
private void grow(int minCapacity) {
|
jaroslav@597
|
255 |
// overflow-conscious code
|
jaroslav@597
|
256 |
int oldCapacity = elementData.length;
|
jaroslav@597
|
257 |
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
|
jaroslav@597
|
258 |
capacityIncrement : oldCapacity);
|
jaroslav@597
|
259 |
if (newCapacity - minCapacity < 0)
|
jaroslav@597
|
260 |
newCapacity = minCapacity;
|
jaroslav@597
|
261 |
if (newCapacity - MAX_ARRAY_SIZE > 0)
|
jaroslav@597
|
262 |
newCapacity = hugeCapacity(minCapacity);
|
jaroslav@597
|
263 |
elementData = Arrays.copyOf(elementData, newCapacity);
|
jaroslav@597
|
264 |
}
|
jaroslav@597
|
265 |
|
jaroslav@597
|
266 |
private static int hugeCapacity(int minCapacity) {
|
jaroslav@597
|
267 |
if (minCapacity < 0) // overflow
|
jaroslav@597
|
268 |
throw new OutOfMemoryError();
|
jaroslav@597
|
269 |
return (minCapacity > MAX_ARRAY_SIZE) ?
|
jaroslav@597
|
270 |
Integer.MAX_VALUE :
|
jaroslav@597
|
271 |
MAX_ARRAY_SIZE;
|
jaroslav@597
|
272 |
}
|
jaroslav@597
|
273 |
|
jaroslav@597
|
274 |
/**
|
jaroslav@597
|
275 |
* Sets the size of this vector. If the new size is greater than the
|
jaroslav@597
|
276 |
* current size, new {@code null} items are added to the end of
|
jaroslav@597
|
277 |
* the vector. If the new size is less than the current size, all
|
jaroslav@597
|
278 |
* components at index {@code newSize} and greater are discarded.
|
jaroslav@597
|
279 |
*
|
jaroslav@597
|
280 |
* @param newSize the new size of this vector
|
jaroslav@597
|
281 |
* @throws ArrayIndexOutOfBoundsException if the new size is negative
|
jaroslav@597
|
282 |
*/
|
jaroslav@597
|
283 |
public synchronized void setSize(int newSize) {
|
jaroslav@597
|
284 |
modCount++;
|
jaroslav@597
|
285 |
if (newSize > elementCount) {
|
jaroslav@597
|
286 |
ensureCapacityHelper(newSize);
|
jaroslav@597
|
287 |
} else {
|
jaroslav@597
|
288 |
for (int i = newSize ; i < elementCount ; i++) {
|
jaroslav@597
|
289 |
elementData[i] = null;
|
jaroslav@597
|
290 |
}
|
jaroslav@597
|
291 |
}
|
jaroslav@597
|
292 |
elementCount = newSize;
|
jaroslav@597
|
293 |
}
|
jaroslav@597
|
294 |
|
jaroslav@597
|
295 |
/**
|
jaroslav@597
|
296 |
* Returns the current capacity of this vector.
|
jaroslav@597
|
297 |
*
|
jaroslav@597
|
298 |
* @return the current capacity (the length of its internal
|
jaroslav@597
|
299 |
* data array, kept in the field {@code elementData}
|
jaroslav@597
|
300 |
* of this vector)
|
jaroslav@597
|
301 |
*/
|
jaroslav@597
|
302 |
public synchronized int capacity() {
|
jaroslav@597
|
303 |
return elementData.length;
|
jaroslav@597
|
304 |
}
|
jaroslav@597
|
305 |
|
jaroslav@597
|
306 |
/**
|
jaroslav@597
|
307 |
* Returns the number of components in this vector.
|
jaroslav@597
|
308 |
*
|
jaroslav@597
|
309 |
* @return the number of components in this vector
|
jaroslav@597
|
310 |
*/
|
jaroslav@597
|
311 |
public synchronized int size() {
|
jaroslav@597
|
312 |
return elementCount;
|
jaroslav@597
|
313 |
}
|
jaroslav@597
|
314 |
|
jaroslav@597
|
315 |
/**
|
jaroslav@597
|
316 |
* Tests if this vector has no components.
|
jaroslav@597
|
317 |
*
|
jaroslav@597
|
318 |
* @return {@code true} if and only if this vector has
|
jaroslav@597
|
319 |
* no components, that is, its size is zero;
|
jaroslav@597
|
320 |
* {@code false} otherwise.
|
jaroslav@597
|
321 |
*/
|
jaroslav@597
|
322 |
public synchronized boolean isEmpty() {
|
jaroslav@597
|
323 |
return elementCount == 0;
|
jaroslav@597
|
324 |
}
|
jaroslav@597
|
325 |
|
jaroslav@597
|
326 |
/**
|
jaroslav@597
|
327 |
* Returns an enumeration of the components of this vector. The
|
jaroslav@597
|
328 |
* returned {@code Enumeration} object will generate all items in
|
jaroslav@597
|
329 |
* this vector. The first item generated is the item at index {@code 0},
|
jaroslav@597
|
330 |
* then the item at index {@code 1}, and so on.
|
jaroslav@597
|
331 |
*
|
jaroslav@597
|
332 |
* @return an enumeration of the components of this vector
|
jaroslav@597
|
333 |
* @see Iterator
|
jaroslav@597
|
334 |
*/
|
jaroslav@597
|
335 |
public Enumeration<E> elements() {
|
jaroslav@597
|
336 |
return new Enumeration<E>() {
|
jaroslav@597
|
337 |
int count = 0;
|
jaroslav@597
|
338 |
|
jaroslav@597
|
339 |
public boolean hasMoreElements() {
|
jaroslav@597
|
340 |
return count < elementCount;
|
jaroslav@597
|
341 |
}
|
jaroslav@597
|
342 |
|
jaroslav@597
|
343 |
public E nextElement() {
|
jaroslav@597
|
344 |
synchronized (Vector.this) {
|
jaroslav@597
|
345 |
if (count < elementCount) {
|
jaroslav@597
|
346 |
return elementData(count++);
|
jaroslav@597
|
347 |
}
|
jaroslav@597
|
348 |
}
|
jaroslav@597
|
349 |
throw new NoSuchElementException("Vector Enumeration");
|
jaroslav@597
|
350 |
}
|
jaroslav@597
|
351 |
};
|
jaroslav@597
|
352 |
}
|
jaroslav@597
|
353 |
|
jaroslav@597
|
354 |
/**
|
jaroslav@597
|
355 |
* Returns {@code true} if this vector contains the specified element.
|
jaroslav@597
|
356 |
* More formally, returns {@code true} if and only if this vector
|
jaroslav@597
|
357 |
* contains at least one element {@code e} such that
|
jaroslav@597
|
358 |
* <tt>(o==null ? e==null : o.equals(e))</tt>.
|
jaroslav@597
|
359 |
*
|
jaroslav@597
|
360 |
* @param o element whose presence in this vector is to be tested
|
jaroslav@597
|
361 |
* @return {@code true} if this vector contains the specified element
|
jaroslav@597
|
362 |
*/
|
jaroslav@597
|
363 |
public boolean contains(Object o) {
|
jaroslav@597
|
364 |
return indexOf(o, 0) >= 0;
|
jaroslav@597
|
365 |
}
|
jaroslav@597
|
366 |
|
jaroslav@597
|
367 |
/**
|
jaroslav@597
|
368 |
* Returns the index of the first occurrence of the specified element
|
jaroslav@597
|
369 |
* in this vector, or -1 if this vector does not contain the element.
|
jaroslav@597
|
370 |
* More formally, returns the lowest index {@code i} such that
|
jaroslav@597
|
371 |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
|
jaroslav@597
|
372 |
* or -1 if there is no such index.
|
jaroslav@597
|
373 |
*
|
jaroslav@597
|
374 |
* @param o element to search for
|
jaroslav@597
|
375 |
* @return the index of the first occurrence of the specified element in
|
jaroslav@597
|
376 |
* this vector, or -1 if this vector does not contain the element
|
jaroslav@597
|
377 |
*/
|
jaroslav@597
|
378 |
public int indexOf(Object o) {
|
jaroslav@597
|
379 |
return indexOf(o, 0);
|
jaroslav@597
|
380 |
}
|
jaroslav@597
|
381 |
|
jaroslav@597
|
382 |
/**
|
jaroslav@597
|
383 |
* Returns the index of the first occurrence of the specified element in
|
jaroslav@597
|
384 |
* this vector, searching forwards from {@code index}, or returns -1 if
|
jaroslav@597
|
385 |
* the element is not found.
|
jaroslav@597
|
386 |
* More formally, returns the lowest index {@code i} such that
|
jaroslav@597
|
387 |
* <tt>(i >= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>,
|
jaroslav@597
|
388 |
* or -1 if there is no such index.
|
jaroslav@597
|
389 |
*
|
jaroslav@597
|
390 |
* @param o element to search for
|
jaroslav@597
|
391 |
* @param index index to start searching from
|
jaroslav@597
|
392 |
* @return the index of the first occurrence of the element in
|
jaroslav@597
|
393 |
* this vector at position {@code index} or later in the vector;
|
jaroslav@597
|
394 |
* {@code -1} if the element is not found.
|
jaroslav@597
|
395 |
* @throws IndexOutOfBoundsException if the specified index is negative
|
jaroslav@597
|
396 |
* @see Object#equals(Object)
|
jaroslav@597
|
397 |
*/
|
jaroslav@597
|
398 |
public synchronized int indexOf(Object o, int index) {
|
jaroslav@597
|
399 |
if (o == null) {
|
jaroslav@597
|
400 |
for (int i = index ; i < elementCount ; i++)
|
jaroslav@597
|
401 |
if (elementData[i]==null)
|
jaroslav@597
|
402 |
return i;
|
jaroslav@597
|
403 |
} else {
|
jaroslav@597
|
404 |
for (int i = index ; i < elementCount ; i++)
|
jaroslav@597
|
405 |
if (o.equals(elementData[i]))
|
jaroslav@597
|
406 |
return i;
|
jaroslav@597
|
407 |
}
|
jaroslav@597
|
408 |
return -1;
|
jaroslav@597
|
409 |
}
|
jaroslav@597
|
410 |
|
jaroslav@597
|
411 |
/**
|
jaroslav@597
|
412 |
* Returns the index of the last occurrence of the specified element
|
jaroslav@597
|
413 |
* in this vector, or -1 if this vector does not contain the element.
|
jaroslav@597
|
414 |
* More formally, returns the highest index {@code i} such that
|
jaroslav@597
|
415 |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>,
|
jaroslav@597
|
416 |
* or -1 if there is no such index.
|
jaroslav@597
|
417 |
*
|
jaroslav@597
|
418 |
* @param o element to search for
|
jaroslav@597
|
419 |
* @return the index of the last occurrence of the specified element in
|
jaroslav@597
|
420 |
* this vector, or -1 if this vector does not contain the element
|
jaroslav@597
|
421 |
*/
|
jaroslav@597
|
422 |
public synchronized int lastIndexOf(Object o) {
|
jaroslav@597
|
423 |
return lastIndexOf(o, elementCount-1);
|
jaroslav@597
|
424 |
}
|
jaroslav@597
|
425 |
|
jaroslav@597
|
426 |
/**
|
jaroslav@597
|
427 |
* Returns the index of the last occurrence of the specified element in
|
jaroslav@597
|
428 |
* this vector, searching backwards from {@code index}, or returns -1 if
|
jaroslav@597
|
429 |
* the element is not found.
|
jaroslav@597
|
430 |
* More formally, returns the highest index {@code i} such that
|
jaroslav@597
|
431 |
* <tt>(i <= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>,
|
jaroslav@597
|
432 |
* or -1 if there is no such index.
|
jaroslav@597
|
433 |
*
|
jaroslav@597
|
434 |
* @param o element to search for
|
jaroslav@597
|
435 |
* @param index index to start searching backwards from
|
jaroslav@597
|
436 |
* @return the index of the last occurrence of the element at position
|
jaroslav@597
|
437 |
* less than or equal to {@code index} in this vector;
|
jaroslav@597
|
438 |
* -1 if the element is not found.
|
jaroslav@597
|
439 |
* @throws IndexOutOfBoundsException if the specified index is greater
|
jaroslav@597
|
440 |
* than or equal to the current size of this vector
|
jaroslav@597
|
441 |
*/
|
jaroslav@597
|
442 |
public synchronized int lastIndexOf(Object o, int index) {
|
jaroslav@597
|
443 |
if (index >= elementCount)
|
jaroslav@597
|
444 |
throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
|
jaroslav@597
|
445 |
|
jaroslav@597
|
446 |
if (o == null) {
|
jaroslav@597
|
447 |
for (int i = index; i >= 0; i--)
|
jaroslav@597
|
448 |
if (elementData[i]==null)
|
jaroslav@597
|
449 |
return i;
|
jaroslav@597
|
450 |
} else {
|
jaroslav@597
|
451 |
for (int i = index; i >= 0; i--)
|
jaroslav@597
|
452 |
if (o.equals(elementData[i]))
|
jaroslav@597
|
453 |
return i;
|
jaroslav@597
|
454 |
}
|
jaroslav@597
|
455 |
return -1;
|
jaroslav@597
|
456 |
}
|
jaroslav@597
|
457 |
|
jaroslav@597
|
458 |
/**
|
jaroslav@597
|
459 |
* Returns the component at the specified index.
|
jaroslav@597
|
460 |
*
|
jaroslav@597
|
461 |
* <p>This method is identical in functionality to the {@link #get(int)}
|
jaroslav@597
|
462 |
* method (which is part of the {@link List} interface).
|
jaroslav@597
|
463 |
*
|
jaroslav@597
|
464 |
* @param index an index into this vector
|
jaroslav@597
|
465 |
* @return the component at the specified index
|
jaroslav@597
|
466 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
467 |
* ({@code index < 0 || index >= size()})
|
jaroslav@597
|
468 |
*/
|
jaroslav@597
|
469 |
public synchronized E elementAt(int index) {
|
jaroslav@597
|
470 |
if (index >= elementCount) {
|
jaroslav@597
|
471 |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
|
jaroslav@597
|
472 |
}
|
jaroslav@597
|
473 |
|
jaroslav@597
|
474 |
return elementData(index);
|
jaroslav@597
|
475 |
}
|
jaroslav@597
|
476 |
|
jaroslav@597
|
477 |
/**
|
jaroslav@597
|
478 |
* Returns the first component (the item at index {@code 0}) of
|
jaroslav@597
|
479 |
* this vector.
|
jaroslav@597
|
480 |
*
|
jaroslav@597
|
481 |
* @return the first component of this vector
|
jaroslav@597
|
482 |
* @throws NoSuchElementException if this vector has no components
|
jaroslav@597
|
483 |
*/
|
jaroslav@597
|
484 |
public synchronized E firstElement() {
|
jaroslav@597
|
485 |
if (elementCount == 0) {
|
jaroslav@597
|
486 |
throw new NoSuchElementException();
|
jaroslav@597
|
487 |
}
|
jaroslav@597
|
488 |
return elementData(0);
|
jaroslav@597
|
489 |
}
|
jaroslav@597
|
490 |
|
jaroslav@597
|
491 |
/**
|
jaroslav@597
|
492 |
* Returns the last component of the vector.
|
jaroslav@597
|
493 |
*
|
jaroslav@597
|
494 |
* @return the last component of the vector, i.e., the component at index
|
jaroslav@597
|
495 |
* <code>size() - 1</code>.
|
jaroslav@597
|
496 |
* @throws NoSuchElementException if this vector is empty
|
jaroslav@597
|
497 |
*/
|
jaroslav@597
|
498 |
public synchronized E lastElement() {
|
jaroslav@597
|
499 |
if (elementCount == 0) {
|
jaroslav@597
|
500 |
throw new NoSuchElementException();
|
jaroslav@597
|
501 |
}
|
jaroslav@597
|
502 |
return elementData(elementCount - 1);
|
jaroslav@597
|
503 |
}
|
jaroslav@597
|
504 |
|
jaroslav@597
|
505 |
/**
|
jaroslav@597
|
506 |
* Sets the component at the specified {@code index} of this
|
jaroslav@597
|
507 |
* vector to be the specified object. The previous component at that
|
jaroslav@597
|
508 |
* position is discarded.
|
jaroslav@597
|
509 |
*
|
jaroslav@597
|
510 |
* <p>The index must be a value greater than or equal to {@code 0}
|
jaroslav@597
|
511 |
* and less than the current size of the vector.
|
jaroslav@597
|
512 |
*
|
jaroslav@597
|
513 |
* <p>This method is identical in functionality to the
|
jaroslav@597
|
514 |
* {@link #set(int, Object) set(int, E)}
|
jaroslav@597
|
515 |
* method (which is part of the {@link List} interface). Note that the
|
jaroslav@597
|
516 |
* {@code set} method reverses the order of the parameters, to more closely
|
jaroslav@597
|
517 |
* match array usage. Note also that the {@code set} method returns the
|
jaroslav@597
|
518 |
* old value that was stored at the specified position.
|
jaroslav@597
|
519 |
*
|
jaroslav@597
|
520 |
* @param obj what the component is to be set to
|
jaroslav@597
|
521 |
* @param index the specified index
|
jaroslav@597
|
522 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
523 |
* ({@code index < 0 || index >= size()})
|
jaroslav@597
|
524 |
*/
|
jaroslav@597
|
525 |
public synchronized void setElementAt(E obj, int index) {
|
jaroslav@597
|
526 |
if (index >= elementCount) {
|
jaroslav@597
|
527 |
throw new ArrayIndexOutOfBoundsException(index + " >= " +
|
jaroslav@597
|
528 |
elementCount);
|
jaroslav@597
|
529 |
}
|
jaroslav@597
|
530 |
elementData[index] = obj;
|
jaroslav@597
|
531 |
}
|
jaroslav@597
|
532 |
|
jaroslav@597
|
533 |
/**
|
jaroslav@597
|
534 |
* Deletes the component at the specified index. Each component in
|
jaroslav@597
|
535 |
* this vector with an index greater or equal to the specified
|
jaroslav@597
|
536 |
* {@code index} is shifted downward to have an index one
|
jaroslav@597
|
537 |
* smaller than the value it had previously. The size of this vector
|
jaroslav@597
|
538 |
* is decreased by {@code 1}.
|
jaroslav@597
|
539 |
*
|
jaroslav@597
|
540 |
* <p>The index must be a value greater than or equal to {@code 0}
|
jaroslav@597
|
541 |
* and less than the current size of the vector.
|
jaroslav@597
|
542 |
*
|
jaroslav@597
|
543 |
* <p>This method is identical in functionality to the {@link #remove(int)}
|
jaroslav@597
|
544 |
* method (which is part of the {@link List} interface). Note that the
|
jaroslav@597
|
545 |
* {@code remove} method returns the old value that was stored at the
|
jaroslav@597
|
546 |
* specified position.
|
jaroslav@597
|
547 |
*
|
jaroslav@597
|
548 |
* @param index the index of the object to remove
|
jaroslav@597
|
549 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
550 |
* ({@code index < 0 || index >= size()})
|
jaroslav@597
|
551 |
*/
|
jaroslav@597
|
552 |
public synchronized void removeElementAt(int index) {
|
jaroslav@597
|
553 |
modCount++;
|
jaroslav@597
|
554 |
if (index >= elementCount) {
|
jaroslav@597
|
555 |
throw new ArrayIndexOutOfBoundsException(index + " >= " +
|
jaroslav@597
|
556 |
elementCount);
|
jaroslav@597
|
557 |
}
|
jaroslav@597
|
558 |
else if (index < 0) {
|
jaroslav@597
|
559 |
throw new ArrayIndexOutOfBoundsException(index);
|
jaroslav@597
|
560 |
}
|
jaroslav@597
|
561 |
int j = elementCount - index - 1;
|
jaroslav@597
|
562 |
if (j > 0) {
|
jaroslav@597
|
563 |
System.arraycopy(elementData, index + 1, elementData, index, j);
|
jaroslav@597
|
564 |
}
|
jaroslav@597
|
565 |
elementCount--;
|
jaroslav@597
|
566 |
elementData[elementCount] = null; /* to let gc do its work */
|
jaroslav@597
|
567 |
}
|
jaroslav@597
|
568 |
|
jaroslav@597
|
569 |
/**
|
jaroslav@597
|
570 |
* Inserts the specified object as a component in this vector at the
|
jaroslav@597
|
571 |
* specified {@code index}. Each component in this vector with
|
jaroslav@597
|
572 |
* an index greater or equal to the specified {@code index} is
|
jaroslav@597
|
573 |
* shifted upward to have an index one greater than the value it had
|
jaroslav@597
|
574 |
* previously.
|
jaroslav@597
|
575 |
*
|
jaroslav@597
|
576 |
* <p>The index must be a value greater than or equal to {@code 0}
|
jaroslav@597
|
577 |
* and less than or equal to the current size of the vector. (If the
|
jaroslav@597
|
578 |
* index is equal to the current size of the vector, the new element
|
jaroslav@597
|
579 |
* is appended to the Vector.)
|
jaroslav@597
|
580 |
*
|
jaroslav@597
|
581 |
* <p>This method is identical in functionality to the
|
jaroslav@597
|
582 |
* {@link #add(int, Object) add(int, E)}
|
jaroslav@597
|
583 |
* method (which is part of the {@link List} interface). Note that the
|
jaroslav@597
|
584 |
* {@code add} method reverses the order of the parameters, to more closely
|
jaroslav@597
|
585 |
* match array usage.
|
jaroslav@597
|
586 |
*
|
jaroslav@597
|
587 |
* @param obj the component to insert
|
jaroslav@597
|
588 |
* @param index where to insert the new component
|
jaroslav@597
|
589 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
590 |
* ({@code index < 0 || index > size()})
|
jaroslav@597
|
591 |
*/
|
jaroslav@597
|
592 |
public synchronized void insertElementAt(E obj, int index) {
|
jaroslav@597
|
593 |
modCount++;
|
jaroslav@597
|
594 |
if (index > elementCount) {
|
jaroslav@597
|
595 |
throw new ArrayIndexOutOfBoundsException(index
|
jaroslav@597
|
596 |
+ " > " + elementCount);
|
jaroslav@597
|
597 |
}
|
jaroslav@597
|
598 |
ensureCapacityHelper(elementCount + 1);
|
jaroslav@597
|
599 |
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
|
jaroslav@597
|
600 |
elementData[index] = obj;
|
jaroslav@597
|
601 |
elementCount++;
|
jaroslav@597
|
602 |
}
|
jaroslav@597
|
603 |
|
jaroslav@597
|
604 |
/**
|
jaroslav@597
|
605 |
* Adds the specified component to the end of this vector,
|
jaroslav@597
|
606 |
* increasing its size by one. The capacity of this vector is
|
jaroslav@597
|
607 |
* increased if its size becomes greater than its capacity.
|
jaroslav@597
|
608 |
*
|
jaroslav@597
|
609 |
* <p>This method is identical in functionality to the
|
jaroslav@597
|
610 |
* {@link #add(Object) add(E)}
|
jaroslav@597
|
611 |
* method (which is part of the {@link List} interface).
|
jaroslav@597
|
612 |
*
|
jaroslav@597
|
613 |
* @param obj the component to be added
|
jaroslav@597
|
614 |
*/
|
jaroslav@597
|
615 |
public synchronized void addElement(E obj) {
|
jaroslav@597
|
616 |
modCount++;
|
jaroslav@597
|
617 |
ensureCapacityHelper(elementCount + 1);
|
jaroslav@597
|
618 |
elementData[elementCount++] = obj;
|
jaroslav@597
|
619 |
}
|
jaroslav@597
|
620 |
|
jaroslav@597
|
621 |
/**
|
jaroslav@597
|
622 |
* Removes the first (lowest-indexed) occurrence of the argument
|
jaroslav@597
|
623 |
* from this vector. If the object is found in this vector, each
|
jaroslav@597
|
624 |
* component in the vector with an index greater or equal to the
|
jaroslav@597
|
625 |
* object's index is shifted downward to have an index one smaller
|
jaroslav@597
|
626 |
* than the value it had previously.
|
jaroslav@597
|
627 |
*
|
jaroslav@597
|
628 |
* <p>This method is identical in functionality to the
|
jaroslav@597
|
629 |
* {@link #remove(Object)} method (which is part of the
|
jaroslav@597
|
630 |
* {@link List} interface).
|
jaroslav@597
|
631 |
*
|
jaroslav@597
|
632 |
* @param obj the component to be removed
|
jaroslav@597
|
633 |
* @return {@code true} if the argument was a component of this
|
jaroslav@597
|
634 |
* vector; {@code false} otherwise.
|
jaroslav@597
|
635 |
*/
|
jaroslav@597
|
636 |
public synchronized boolean removeElement(Object obj) {
|
jaroslav@597
|
637 |
modCount++;
|
jaroslav@597
|
638 |
int i = indexOf(obj);
|
jaroslav@597
|
639 |
if (i >= 0) {
|
jaroslav@597
|
640 |
removeElementAt(i);
|
jaroslav@597
|
641 |
return true;
|
jaroslav@597
|
642 |
}
|
jaroslav@597
|
643 |
return false;
|
jaroslav@597
|
644 |
}
|
jaroslav@597
|
645 |
|
jaroslav@597
|
646 |
/**
|
jaroslav@597
|
647 |
* Removes all components from this vector and sets its size to zero.
|
jaroslav@597
|
648 |
*
|
jaroslav@597
|
649 |
* <p>This method is identical in functionality to the {@link #clear}
|
jaroslav@597
|
650 |
* method (which is part of the {@link List} interface).
|
jaroslav@597
|
651 |
*/
|
jaroslav@597
|
652 |
public synchronized void removeAllElements() {
|
jaroslav@597
|
653 |
modCount++;
|
jaroslav@597
|
654 |
// Let gc do its work
|
jaroslav@597
|
655 |
for (int i = 0; i < elementCount; i++)
|
jaroslav@597
|
656 |
elementData[i] = null;
|
jaroslav@597
|
657 |
|
jaroslav@597
|
658 |
elementCount = 0;
|
jaroslav@597
|
659 |
}
|
jaroslav@597
|
660 |
|
jaroslav@597
|
661 |
/**
|
jaroslav@597
|
662 |
* Returns a clone of this vector. The copy will contain a
|
jaroslav@597
|
663 |
* reference to a clone of the internal data array, not a reference
|
jaroslav@597
|
664 |
* to the original internal data array of this {@code Vector} object.
|
jaroslav@597
|
665 |
*
|
jaroslav@597
|
666 |
* @return a clone of this vector
|
jaroslav@597
|
667 |
*/
|
jaroslav@597
|
668 |
public synchronized Object clone() {
|
jaroslav@597
|
669 |
try {
|
jaroslav@597
|
670 |
@SuppressWarnings("unchecked")
|
jaroslav@597
|
671 |
Vector<E> v = (Vector<E>) super.clone();
|
jaroslav@597
|
672 |
v.elementData = Arrays.copyOf(elementData, elementCount);
|
jaroslav@597
|
673 |
v.modCount = 0;
|
jaroslav@597
|
674 |
return v;
|
jaroslav@597
|
675 |
} catch (CloneNotSupportedException e) {
|
jaroslav@597
|
676 |
// this shouldn't happen, since we are Cloneable
|
jaroslav@597
|
677 |
throw new InternalError();
|
jaroslav@597
|
678 |
}
|
jaroslav@597
|
679 |
}
|
jaroslav@597
|
680 |
|
jaroslav@597
|
681 |
/**
|
jaroslav@597
|
682 |
* Returns an array containing all of the elements in this Vector
|
jaroslav@597
|
683 |
* in the correct order.
|
jaroslav@597
|
684 |
*
|
jaroslav@597
|
685 |
* @since 1.2
|
jaroslav@597
|
686 |
*/
|
jaroslav@597
|
687 |
public synchronized Object[] toArray() {
|
jaroslav@597
|
688 |
return Arrays.copyOf(elementData, elementCount);
|
jaroslav@597
|
689 |
}
|
jaroslav@597
|
690 |
|
jaroslav@597
|
691 |
/**
|
jaroslav@597
|
692 |
* Returns an array containing all of the elements in this Vector in the
|
jaroslav@597
|
693 |
* correct order; the runtime type of the returned array is that of the
|
jaroslav@597
|
694 |
* specified array. If the Vector fits in the specified array, it is
|
jaroslav@597
|
695 |
* returned therein. Otherwise, a new array is allocated with the runtime
|
jaroslav@597
|
696 |
* type of the specified array and the size of this Vector.
|
jaroslav@597
|
697 |
*
|
jaroslav@597
|
698 |
* <p>If the Vector fits in the specified array with room to spare
|
jaroslav@597
|
699 |
* (i.e., the array has more elements than the Vector),
|
jaroslav@597
|
700 |
* the element in the array immediately following the end of the
|
jaroslav@597
|
701 |
* Vector is set to null. (This is useful in determining the length
|
jaroslav@597
|
702 |
* of the Vector <em>only</em> if the caller knows that the Vector
|
jaroslav@597
|
703 |
* does not contain any null elements.)
|
jaroslav@597
|
704 |
*
|
jaroslav@597
|
705 |
* @param a the array into which the elements of the Vector are to
|
jaroslav@597
|
706 |
* be stored, if it is big enough; otherwise, a new array of the
|
jaroslav@597
|
707 |
* same runtime type is allocated for this purpose.
|
jaroslav@597
|
708 |
* @return an array containing the elements of the Vector
|
jaroslav@597
|
709 |
* @throws ArrayStoreException if the runtime type of a is not a supertype
|
jaroslav@597
|
710 |
* of the runtime type of every element in this Vector
|
jaroslav@597
|
711 |
* @throws NullPointerException if the given array is null
|
jaroslav@597
|
712 |
* @since 1.2
|
jaroslav@597
|
713 |
*/
|
jaroslav@597
|
714 |
@SuppressWarnings("unchecked")
|
jaroslav@597
|
715 |
public synchronized <T> T[] toArray(T[] a) {
|
jaroslav@597
|
716 |
if (a.length < elementCount)
|
jaroslav@597
|
717 |
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
|
jaroslav@597
|
718 |
|
jaroslav@597
|
719 |
System.arraycopy(elementData, 0, a, 0, elementCount);
|
jaroslav@597
|
720 |
|
jaroslav@597
|
721 |
if (a.length > elementCount)
|
jaroslav@597
|
722 |
a[elementCount] = null;
|
jaroslav@597
|
723 |
|
jaroslav@597
|
724 |
return a;
|
jaroslav@597
|
725 |
}
|
jaroslav@597
|
726 |
|
jaroslav@597
|
727 |
// Positional Access Operations
|
jaroslav@597
|
728 |
|
jaroslav@597
|
729 |
@SuppressWarnings("unchecked")
|
jaroslav@597
|
730 |
E elementData(int index) {
|
jaroslav@597
|
731 |
return (E) elementData[index];
|
jaroslav@597
|
732 |
}
|
jaroslav@597
|
733 |
|
jaroslav@597
|
734 |
/**
|
jaroslav@597
|
735 |
* Returns the element at the specified position in this Vector.
|
jaroslav@597
|
736 |
*
|
jaroslav@597
|
737 |
* @param index index of the element to return
|
jaroslav@597
|
738 |
* @return object at the specified index
|
jaroslav@597
|
739 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
740 |
* ({@code index < 0 || index >= size()})
|
jaroslav@597
|
741 |
* @since 1.2
|
jaroslav@597
|
742 |
*/
|
jaroslav@597
|
743 |
public synchronized E get(int index) {
|
jaroslav@597
|
744 |
if (index >= elementCount)
|
jaroslav@597
|
745 |
throw new ArrayIndexOutOfBoundsException(index);
|
jaroslav@597
|
746 |
|
jaroslav@597
|
747 |
return elementData(index);
|
jaroslav@597
|
748 |
}
|
jaroslav@597
|
749 |
|
jaroslav@597
|
750 |
/**
|
jaroslav@597
|
751 |
* Replaces the element at the specified position in this Vector with the
|
jaroslav@597
|
752 |
* specified element.
|
jaroslav@597
|
753 |
*
|
jaroslav@597
|
754 |
* @param index index of the element to replace
|
jaroslav@597
|
755 |
* @param element element to be stored at the specified position
|
jaroslav@597
|
756 |
* @return the element previously at the specified position
|
jaroslav@597
|
757 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
758 |
* ({@code index < 0 || index >= size()})
|
jaroslav@597
|
759 |
* @since 1.2
|
jaroslav@597
|
760 |
*/
|
jaroslav@597
|
761 |
public synchronized E set(int index, E element) {
|
jaroslav@597
|
762 |
if (index >= elementCount)
|
jaroslav@597
|
763 |
throw new ArrayIndexOutOfBoundsException(index);
|
jaroslav@597
|
764 |
|
jaroslav@597
|
765 |
E oldValue = elementData(index);
|
jaroslav@597
|
766 |
elementData[index] = element;
|
jaroslav@597
|
767 |
return oldValue;
|
jaroslav@597
|
768 |
}
|
jaroslav@597
|
769 |
|
jaroslav@597
|
770 |
/**
|
jaroslav@597
|
771 |
* Appends the specified element to the end of this Vector.
|
jaroslav@597
|
772 |
*
|
jaroslav@597
|
773 |
* @param e element to be appended to this Vector
|
jaroslav@597
|
774 |
* @return {@code true} (as specified by {@link Collection#add})
|
jaroslav@597
|
775 |
* @since 1.2
|
jaroslav@597
|
776 |
*/
|
jaroslav@597
|
777 |
public synchronized boolean add(E e) {
|
jaroslav@597
|
778 |
modCount++;
|
jaroslav@597
|
779 |
ensureCapacityHelper(elementCount + 1);
|
jaroslav@597
|
780 |
elementData[elementCount++] = e;
|
jaroslav@597
|
781 |
return true;
|
jaroslav@597
|
782 |
}
|
jaroslav@597
|
783 |
|
jaroslav@597
|
784 |
/**
|
jaroslav@597
|
785 |
* Removes the first occurrence of the specified element in this Vector
|
jaroslav@597
|
786 |
* If the Vector does not contain the element, it is unchanged. More
|
jaroslav@597
|
787 |
* formally, removes the element with the lowest index i such that
|
jaroslav@597
|
788 |
* {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
|
jaroslav@597
|
789 |
* an element exists).
|
jaroslav@597
|
790 |
*
|
jaroslav@597
|
791 |
* @param o element to be removed from this Vector, if present
|
jaroslav@597
|
792 |
* @return true if the Vector contained the specified element
|
jaroslav@597
|
793 |
* @since 1.2
|
jaroslav@597
|
794 |
*/
|
jaroslav@597
|
795 |
public boolean remove(Object o) {
|
jaroslav@597
|
796 |
return removeElement(o);
|
jaroslav@597
|
797 |
}
|
jaroslav@597
|
798 |
|
jaroslav@597
|
799 |
/**
|
jaroslav@597
|
800 |
* Inserts the specified element at the specified position in this Vector.
|
jaroslav@597
|
801 |
* Shifts the element currently at that position (if any) and any
|
jaroslav@597
|
802 |
* subsequent elements to the right (adds one to their indices).
|
jaroslav@597
|
803 |
*
|
jaroslav@597
|
804 |
* @param index index at which the specified element is to be inserted
|
jaroslav@597
|
805 |
* @param element element to be inserted
|
jaroslav@597
|
806 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
807 |
* ({@code index < 0 || index > size()})
|
jaroslav@597
|
808 |
* @since 1.2
|
jaroslav@597
|
809 |
*/
|
jaroslav@597
|
810 |
public void add(int index, E element) {
|
jaroslav@597
|
811 |
insertElementAt(element, index);
|
jaroslav@597
|
812 |
}
|
jaroslav@597
|
813 |
|
jaroslav@597
|
814 |
/**
|
jaroslav@597
|
815 |
* Removes the element at the specified position in this Vector.
|
jaroslav@597
|
816 |
* Shifts any subsequent elements to the left (subtracts one from their
|
jaroslav@597
|
817 |
* indices). Returns the element that was removed from the Vector.
|
jaroslav@597
|
818 |
*
|
jaroslav@597
|
819 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
820 |
* ({@code index < 0 || index >= size()})
|
jaroslav@597
|
821 |
* @param index the index of the element to be removed
|
jaroslav@597
|
822 |
* @return element that was removed
|
jaroslav@597
|
823 |
* @since 1.2
|
jaroslav@597
|
824 |
*/
|
jaroslav@597
|
825 |
public synchronized E remove(int index) {
|
jaroslav@597
|
826 |
modCount++;
|
jaroslav@597
|
827 |
if (index >= elementCount)
|
jaroslav@597
|
828 |
throw new ArrayIndexOutOfBoundsException(index);
|
jaroslav@597
|
829 |
E oldValue = elementData(index);
|
jaroslav@597
|
830 |
|
jaroslav@597
|
831 |
int numMoved = elementCount - index - 1;
|
jaroslav@597
|
832 |
if (numMoved > 0)
|
jaroslav@597
|
833 |
System.arraycopy(elementData, index+1, elementData, index,
|
jaroslav@597
|
834 |
numMoved);
|
jaroslav@597
|
835 |
elementData[--elementCount] = null; // Let gc do its work
|
jaroslav@597
|
836 |
|
jaroslav@597
|
837 |
return oldValue;
|
jaroslav@597
|
838 |
}
|
jaroslav@597
|
839 |
|
jaroslav@597
|
840 |
/**
|
jaroslav@597
|
841 |
* Removes all of the elements from this Vector. The Vector will
|
jaroslav@597
|
842 |
* be empty after this call returns (unless it throws an exception).
|
jaroslav@597
|
843 |
*
|
jaroslav@597
|
844 |
* @since 1.2
|
jaroslav@597
|
845 |
*/
|
jaroslav@597
|
846 |
public void clear() {
|
jaroslav@597
|
847 |
removeAllElements();
|
jaroslav@597
|
848 |
}
|
jaroslav@597
|
849 |
|
jaroslav@597
|
850 |
// Bulk Operations
|
jaroslav@597
|
851 |
|
jaroslav@597
|
852 |
/**
|
jaroslav@597
|
853 |
* Returns true if this Vector contains all of the elements in the
|
jaroslav@597
|
854 |
* specified Collection.
|
jaroslav@597
|
855 |
*
|
jaroslav@597
|
856 |
* @param c a collection whose elements will be tested for containment
|
jaroslav@597
|
857 |
* in this Vector
|
jaroslav@597
|
858 |
* @return true if this Vector contains all of the elements in the
|
jaroslav@597
|
859 |
* specified collection
|
jaroslav@597
|
860 |
* @throws NullPointerException if the specified collection is null
|
jaroslav@597
|
861 |
*/
|
jaroslav@597
|
862 |
public synchronized boolean containsAll(Collection<?> c) {
|
jaroslav@597
|
863 |
return super.containsAll(c);
|
jaroslav@597
|
864 |
}
|
jaroslav@597
|
865 |
|
jaroslav@597
|
866 |
/**
|
jaroslav@597
|
867 |
* Appends all of the elements in the specified Collection to the end of
|
jaroslav@597
|
868 |
* this Vector, in the order that they are returned by the specified
|
jaroslav@597
|
869 |
* Collection's Iterator. The behavior of this operation is undefined if
|
jaroslav@597
|
870 |
* the specified Collection is modified while the operation is in progress.
|
jaroslav@597
|
871 |
* (This implies that the behavior of this call is undefined if the
|
jaroslav@597
|
872 |
* specified Collection is this Vector, and this Vector is nonempty.)
|
jaroslav@597
|
873 |
*
|
jaroslav@597
|
874 |
* @param c elements to be inserted into this Vector
|
jaroslav@597
|
875 |
* @return {@code true} if this Vector changed as a result of the call
|
jaroslav@597
|
876 |
* @throws NullPointerException if the specified collection is null
|
jaroslav@597
|
877 |
* @since 1.2
|
jaroslav@597
|
878 |
*/
|
jaroslav@597
|
879 |
public synchronized boolean addAll(Collection<? extends E> c) {
|
jaroslav@597
|
880 |
modCount++;
|
jaroslav@597
|
881 |
Object[] a = c.toArray();
|
jaroslav@597
|
882 |
int numNew = a.length;
|
jaroslav@597
|
883 |
ensureCapacityHelper(elementCount + numNew);
|
jaroslav@597
|
884 |
System.arraycopy(a, 0, elementData, elementCount, numNew);
|
jaroslav@597
|
885 |
elementCount += numNew;
|
jaroslav@597
|
886 |
return numNew != 0;
|
jaroslav@597
|
887 |
}
|
jaroslav@597
|
888 |
|
jaroslav@597
|
889 |
/**
|
jaroslav@597
|
890 |
* Removes from this Vector all of its elements that are contained in the
|
jaroslav@597
|
891 |
* specified Collection.
|
jaroslav@597
|
892 |
*
|
jaroslav@597
|
893 |
* @param c a collection of elements to be removed from the Vector
|
jaroslav@597
|
894 |
* @return true if this Vector changed as a result of the call
|
jaroslav@597
|
895 |
* @throws ClassCastException if the types of one or more elements
|
jaroslav@597
|
896 |
* in this vector are incompatible with the specified
|
jaroslav@597
|
897 |
* collection
|
jaroslav@597
|
898 |
* (<a href="Collection.html#optional-restrictions">optional</a>)
|
jaroslav@597
|
899 |
* @throws NullPointerException if this vector contains one or more null
|
jaroslav@597
|
900 |
* elements and the specified collection does not support null
|
jaroslav@597
|
901 |
* elements
|
jaroslav@597
|
902 |
* (<a href="Collection.html#optional-restrictions">optional</a>),
|
jaroslav@597
|
903 |
* or if the specified collection is null
|
jaroslav@597
|
904 |
* @since 1.2
|
jaroslav@597
|
905 |
*/
|
jaroslav@597
|
906 |
public synchronized boolean removeAll(Collection<?> c) {
|
jaroslav@597
|
907 |
return super.removeAll(c);
|
jaroslav@597
|
908 |
}
|
jaroslav@597
|
909 |
|
jaroslav@597
|
910 |
/**
|
jaroslav@597
|
911 |
* Retains only the elements in this Vector that are contained in the
|
jaroslav@597
|
912 |
* specified Collection. In other words, removes from this Vector all
|
jaroslav@597
|
913 |
* of its elements that are not contained in the specified Collection.
|
jaroslav@597
|
914 |
*
|
jaroslav@597
|
915 |
* @param c a collection of elements to be retained in this Vector
|
jaroslav@597
|
916 |
* (all other elements are removed)
|
jaroslav@597
|
917 |
* @return true if this Vector changed as a result of the call
|
jaroslav@597
|
918 |
* @throws ClassCastException if the types of one or more elements
|
jaroslav@597
|
919 |
* in this vector are incompatible with the specified
|
jaroslav@597
|
920 |
* collection
|
jaroslav@597
|
921 |
* (<a href="Collection.html#optional-restrictions">optional</a>)
|
jaroslav@597
|
922 |
* @throws NullPointerException if this vector contains one or more null
|
jaroslav@597
|
923 |
* elements and the specified collection does not support null
|
jaroslav@597
|
924 |
* elements
|
jaroslav@597
|
925 |
* (<a href="Collection.html#optional-restrictions">optional</a>),
|
jaroslav@597
|
926 |
* or if the specified collection is null
|
jaroslav@597
|
927 |
* @since 1.2
|
jaroslav@597
|
928 |
*/
|
jaroslav@597
|
929 |
public synchronized boolean retainAll(Collection<?> c) {
|
jaroslav@597
|
930 |
return super.retainAll(c);
|
jaroslav@597
|
931 |
}
|
jaroslav@597
|
932 |
|
jaroslav@597
|
933 |
/**
|
jaroslav@597
|
934 |
* Inserts all of the elements in the specified Collection into this
|
jaroslav@597
|
935 |
* Vector at the specified position. Shifts the element currently at
|
jaroslav@597
|
936 |
* that position (if any) and any subsequent elements to the right
|
jaroslav@597
|
937 |
* (increases their indices). The new elements will appear in the Vector
|
jaroslav@597
|
938 |
* in the order that they are returned by the specified Collection's
|
jaroslav@597
|
939 |
* iterator.
|
jaroslav@597
|
940 |
*
|
jaroslav@597
|
941 |
* @param index index at which to insert the first element from the
|
jaroslav@597
|
942 |
* specified collection
|
jaroslav@597
|
943 |
* @param c elements to be inserted into this Vector
|
jaroslav@597
|
944 |
* @return {@code true} if this Vector changed as a result of the call
|
jaroslav@597
|
945 |
* @throws ArrayIndexOutOfBoundsException if the index is out of range
|
jaroslav@597
|
946 |
* ({@code index < 0 || index > size()})
|
jaroslav@597
|
947 |
* @throws NullPointerException if the specified collection is null
|
jaroslav@597
|
948 |
* @since 1.2
|
jaroslav@597
|
949 |
*/
|
jaroslav@597
|
950 |
public synchronized boolean addAll(int index, Collection<? extends E> c) {
|
jaroslav@597
|
951 |
modCount++;
|
jaroslav@597
|
952 |
if (index < 0 || index > elementCount)
|
jaroslav@597
|
953 |
throw new ArrayIndexOutOfBoundsException(index);
|
jaroslav@597
|
954 |
|
jaroslav@597
|
955 |
Object[] a = c.toArray();
|
jaroslav@597
|
956 |
int numNew = a.length;
|
jaroslav@597
|
957 |
ensureCapacityHelper(elementCount + numNew);
|
jaroslav@597
|
958 |
|
jaroslav@597
|
959 |
int numMoved = elementCount - index;
|
jaroslav@597
|
960 |
if (numMoved > 0)
|
jaroslav@597
|
961 |
System.arraycopy(elementData, index, elementData, index + numNew,
|
jaroslav@597
|
962 |
numMoved);
|
jaroslav@597
|
963 |
|
jaroslav@597
|
964 |
System.arraycopy(a, 0, elementData, index, numNew);
|
jaroslav@597
|
965 |
elementCount += numNew;
|
jaroslav@597
|
966 |
return numNew != 0;
|
jaroslav@597
|
967 |
}
|
jaroslav@597
|
968 |
|
jaroslav@597
|
969 |
/**
|
jaroslav@597
|
970 |
* Compares the specified Object with this Vector for equality. Returns
|
jaroslav@597
|
971 |
* true if and only if the specified Object is also a List, both Lists
|
jaroslav@597
|
972 |
* have the same size, and all corresponding pairs of elements in the two
|
jaroslav@597
|
973 |
* Lists are <em>equal</em>. (Two elements {@code e1} and
|
jaroslav@597
|
974 |
* {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
|
jaroslav@597
|
975 |
* e1.equals(e2))}.) In other words, two Lists are defined to be
|
jaroslav@597
|
976 |
* equal if they contain the same elements in the same order.
|
jaroslav@597
|
977 |
*
|
jaroslav@597
|
978 |
* @param o the Object to be compared for equality with this Vector
|
jaroslav@597
|
979 |
* @return true if the specified Object is equal to this Vector
|
jaroslav@597
|
980 |
*/
|
jaroslav@597
|
981 |
public synchronized boolean equals(Object o) {
|
jaroslav@597
|
982 |
return super.equals(o);
|
jaroslav@597
|
983 |
}
|
jaroslav@597
|
984 |
|
jaroslav@597
|
985 |
/**
|
jaroslav@597
|
986 |
* Returns the hash code value for this Vector.
|
jaroslav@597
|
987 |
*/
|
jaroslav@597
|
988 |
public synchronized int hashCode() {
|
jaroslav@597
|
989 |
return super.hashCode();
|
jaroslav@597
|
990 |
}
|
jaroslav@597
|
991 |
|
jaroslav@597
|
992 |
/**
|
jaroslav@597
|
993 |
* Returns a string representation of this Vector, containing
|
jaroslav@597
|
994 |
* the String representation of each element.
|
jaroslav@597
|
995 |
*/
|
jaroslav@597
|
996 |
public synchronized String toString() {
|
jaroslav@597
|
997 |
return super.toString();
|
jaroslav@597
|
998 |
}
|
jaroslav@597
|
999 |
|
jaroslav@597
|
1000 |
/**
|
jaroslav@597
|
1001 |
* Returns a view of the portion of this List between fromIndex,
|
jaroslav@597
|
1002 |
* inclusive, and toIndex, exclusive. (If fromIndex and toIndex are
|
jaroslav@597
|
1003 |
* equal, the returned List is empty.) The returned List is backed by this
|
jaroslav@597
|
1004 |
* List, so changes in the returned List are reflected in this List, and
|
jaroslav@597
|
1005 |
* vice-versa. The returned List supports all of the optional List
|
jaroslav@597
|
1006 |
* operations supported by this List.
|
jaroslav@597
|
1007 |
*
|
jaroslav@597
|
1008 |
* <p>This method eliminates the need for explicit range operations (of
|
jaroslav@597
|
1009 |
* the sort that commonly exist for arrays). Any operation that expects
|
jaroslav@597
|
1010 |
* a List can be used as a range operation by operating on a subList view
|
jaroslav@597
|
1011 |
* instead of a whole List. For example, the following idiom
|
jaroslav@597
|
1012 |
* removes a range of elements from a List:
|
jaroslav@597
|
1013 |
* <pre>
|
jaroslav@597
|
1014 |
* list.subList(from, to).clear();
|
jaroslav@597
|
1015 |
* </pre>
|
jaroslav@597
|
1016 |
* Similar idioms may be constructed for indexOf and lastIndexOf,
|
jaroslav@597
|
1017 |
* and all of the algorithms in the Collections class can be applied to
|
jaroslav@597
|
1018 |
* a subList.
|
jaroslav@597
|
1019 |
*
|
jaroslav@597
|
1020 |
* <p>The semantics of the List returned by this method become undefined if
|
jaroslav@597
|
1021 |
* the backing list (i.e., this List) is <i>structurally modified</i> in
|
jaroslav@597
|
1022 |
* any way other than via the returned List. (Structural modifications are
|
jaroslav@597
|
1023 |
* those that change the size of the List, or otherwise perturb it in such
|
jaroslav@597
|
1024 |
* a fashion that iterations in progress may yield incorrect results.)
|
jaroslav@597
|
1025 |
*
|
jaroslav@597
|
1026 |
* @param fromIndex low endpoint (inclusive) of the subList
|
jaroslav@597
|
1027 |
* @param toIndex high endpoint (exclusive) of the subList
|
jaroslav@597
|
1028 |
* @return a view of the specified range within this List
|
jaroslav@597
|
1029 |
* @throws IndexOutOfBoundsException if an endpoint index value is out of range
|
jaroslav@597
|
1030 |
* {@code (fromIndex < 0 || toIndex > size)}
|
jaroslav@597
|
1031 |
* @throws IllegalArgumentException if the endpoint indices are out of order
|
jaroslav@597
|
1032 |
* {@code (fromIndex > toIndex)}
|
jaroslav@597
|
1033 |
*/
|
jaroslav@597
|
1034 |
public synchronized List<E> subList(int fromIndex, int toIndex) {
|
jaroslav@597
|
1035 |
return Collections.synchronizedList(super.subList(fromIndex, toIndex),
|
jaroslav@597
|
1036 |
this);
|
jaroslav@597
|
1037 |
}
|
jaroslav@597
|
1038 |
|
jaroslav@597
|
1039 |
/**
|
jaroslav@597
|
1040 |
* Removes from this list all of the elements whose index is between
|
jaroslav@597
|
1041 |
* {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
|
jaroslav@597
|
1042 |
* Shifts any succeeding elements to the left (reduces their index).
|
jaroslav@597
|
1043 |
* This call shortens the list by {@code (toIndex - fromIndex)} elements.
|
jaroslav@597
|
1044 |
* (If {@code toIndex==fromIndex}, this operation has no effect.)
|
jaroslav@597
|
1045 |
*/
|
jaroslav@597
|
1046 |
protected synchronized void removeRange(int fromIndex, int toIndex) {
|
jaroslav@597
|
1047 |
modCount++;
|
jaroslav@597
|
1048 |
int numMoved = elementCount - toIndex;
|
jaroslav@597
|
1049 |
System.arraycopy(elementData, toIndex, elementData, fromIndex,
|
jaroslav@597
|
1050 |
numMoved);
|
jaroslav@597
|
1051 |
|
jaroslav@597
|
1052 |
// Let gc do its work
|
jaroslav@597
|
1053 |
int newElementCount = elementCount - (toIndex-fromIndex);
|
jaroslav@597
|
1054 |
while (elementCount != newElementCount)
|
jaroslav@597
|
1055 |
elementData[--elementCount] = null;
|
jaroslav@597
|
1056 |
}
|
jaroslav@597
|
1057 |
|
jaroslav@597
|
1058 |
/**
|
jaroslav@597
|
1059 |
* Returns a list iterator over the elements in this list (in proper
|
jaroslav@597
|
1060 |
* sequence), starting at the specified position in the list.
|
jaroslav@597
|
1061 |
* The specified index indicates the first element that would be
|
jaroslav@597
|
1062 |
* returned by an initial call to {@link ListIterator#next next}.
|
jaroslav@597
|
1063 |
* An initial call to {@link ListIterator#previous previous} would
|
jaroslav@597
|
1064 |
* return the element with the specified index minus one.
|
jaroslav@597
|
1065 |
*
|
jaroslav@597
|
1066 |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
|
jaroslav@597
|
1067 |
*
|
jaroslav@597
|
1068 |
* @throws IndexOutOfBoundsException {@inheritDoc}
|
jaroslav@597
|
1069 |
*/
|
jaroslav@597
|
1070 |
public synchronized ListIterator<E> listIterator(int index) {
|
jaroslav@597
|
1071 |
if (index < 0 || index > elementCount)
|
jaroslav@597
|
1072 |
throw new IndexOutOfBoundsException("Index: "+index);
|
jaroslav@597
|
1073 |
return new ListItr(index);
|
jaroslav@597
|
1074 |
}
|
jaroslav@597
|
1075 |
|
jaroslav@597
|
1076 |
/**
|
jaroslav@597
|
1077 |
* Returns a list iterator over the elements in this list (in proper
|
jaroslav@597
|
1078 |
* sequence).
|
jaroslav@597
|
1079 |
*
|
jaroslav@597
|
1080 |
* <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
|
jaroslav@597
|
1081 |
*
|
jaroslav@597
|
1082 |
* @see #listIterator(int)
|
jaroslav@597
|
1083 |
*/
|
jaroslav@597
|
1084 |
public synchronized ListIterator<E> listIterator() {
|
jaroslav@597
|
1085 |
return new ListItr(0);
|
jaroslav@597
|
1086 |
}
|
jaroslav@597
|
1087 |
|
jaroslav@597
|
1088 |
/**
|
jaroslav@597
|
1089 |
* Returns an iterator over the elements in this list in proper sequence.
|
jaroslav@597
|
1090 |
*
|
jaroslav@597
|
1091 |
* <p>The returned iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
|
jaroslav@597
|
1092 |
*
|
jaroslav@597
|
1093 |
* @return an iterator over the elements in this list in proper sequence
|
jaroslav@597
|
1094 |
*/
|
jaroslav@597
|
1095 |
public synchronized Iterator<E> iterator() {
|
jaroslav@597
|
1096 |
return new Itr();
|
jaroslav@597
|
1097 |
}
|
jaroslav@597
|
1098 |
|
jaroslav@597
|
1099 |
/**
|
jaroslav@597
|
1100 |
* An optimized version of AbstractList.Itr
|
jaroslav@597
|
1101 |
*/
|
jaroslav@597
|
1102 |
private class Itr implements Iterator<E> {
|
jaroslav@597
|
1103 |
int cursor; // index of next element to return
|
jaroslav@597
|
1104 |
int lastRet = -1; // index of last element returned; -1 if no such
|
jaroslav@597
|
1105 |
int expectedModCount = modCount;
|
jaroslav@597
|
1106 |
|
jaroslav@597
|
1107 |
public boolean hasNext() {
|
jaroslav@597
|
1108 |
// Racy but within spec, since modifications are checked
|
jaroslav@597
|
1109 |
// within or after synchronization in next/previous
|
jaroslav@597
|
1110 |
return cursor != elementCount;
|
jaroslav@597
|
1111 |
}
|
jaroslav@597
|
1112 |
|
jaroslav@597
|
1113 |
public E next() {
|
jaroslav@597
|
1114 |
synchronized (Vector.this) {
|
jaroslav@597
|
1115 |
checkForComodification();
|
jaroslav@597
|
1116 |
int i = cursor;
|
jaroslav@597
|
1117 |
if (i >= elementCount)
|
jaroslav@597
|
1118 |
throw new NoSuchElementException();
|
jaroslav@597
|
1119 |
cursor = i + 1;
|
jaroslav@597
|
1120 |
return elementData(lastRet = i);
|
jaroslav@597
|
1121 |
}
|
jaroslav@597
|
1122 |
}
|
jaroslav@597
|
1123 |
|
jaroslav@597
|
1124 |
public void remove() {
|
jaroslav@597
|
1125 |
if (lastRet == -1)
|
jaroslav@597
|
1126 |
throw new IllegalStateException();
|
jaroslav@597
|
1127 |
synchronized (Vector.this) {
|
jaroslav@597
|
1128 |
checkForComodification();
|
jaroslav@597
|
1129 |
Vector.this.remove(lastRet);
|
jaroslav@597
|
1130 |
expectedModCount = modCount;
|
jaroslav@597
|
1131 |
}
|
jaroslav@597
|
1132 |
cursor = lastRet;
|
jaroslav@597
|
1133 |
lastRet = -1;
|
jaroslav@597
|
1134 |
}
|
jaroslav@597
|
1135 |
|
jaroslav@597
|
1136 |
final void checkForComodification() {
|
jaroslav@597
|
1137 |
if (modCount != expectedModCount)
|
jaroslav@597
|
1138 |
throw new ConcurrentModificationException();
|
jaroslav@597
|
1139 |
}
|
jaroslav@597
|
1140 |
}
|
jaroslav@597
|
1141 |
|
jaroslav@597
|
1142 |
/**
|
jaroslav@597
|
1143 |
* An optimized version of AbstractList.ListItr
|
jaroslav@597
|
1144 |
*/
|
jaroslav@597
|
1145 |
final class ListItr extends Itr implements ListIterator<E> {
|
jaroslav@597
|
1146 |
ListItr(int index) {
|
jaroslav@597
|
1147 |
super();
|
jaroslav@597
|
1148 |
cursor = index;
|
jaroslav@597
|
1149 |
}
|
jaroslav@597
|
1150 |
|
jaroslav@597
|
1151 |
public boolean hasPrevious() {
|
jaroslav@597
|
1152 |
return cursor != 0;
|
jaroslav@597
|
1153 |
}
|
jaroslav@597
|
1154 |
|
jaroslav@597
|
1155 |
public int nextIndex() {
|
jaroslav@597
|
1156 |
return cursor;
|
jaroslav@597
|
1157 |
}
|
jaroslav@597
|
1158 |
|
jaroslav@597
|
1159 |
public int previousIndex() {
|
jaroslav@597
|
1160 |
return cursor - 1;
|
jaroslav@597
|
1161 |
}
|
jaroslav@597
|
1162 |
|
jaroslav@597
|
1163 |
public E previous() {
|
jaroslav@597
|
1164 |
synchronized (Vector.this) {
|
jaroslav@597
|
1165 |
checkForComodification();
|
jaroslav@597
|
1166 |
int i = cursor - 1;
|
jaroslav@597
|
1167 |
if (i < 0)
|
jaroslav@597
|
1168 |
throw new NoSuchElementException();
|
jaroslav@597
|
1169 |
cursor = i;
|
jaroslav@597
|
1170 |
return elementData(lastRet = i);
|
jaroslav@597
|
1171 |
}
|
jaroslav@597
|
1172 |
}
|
jaroslav@597
|
1173 |
|
jaroslav@597
|
1174 |
public void set(E e) {
|
jaroslav@597
|
1175 |
if (lastRet == -1)
|
jaroslav@597
|
1176 |
throw new IllegalStateException();
|
jaroslav@597
|
1177 |
synchronized (Vector.this) {
|
jaroslav@597
|
1178 |
checkForComodification();
|
jaroslav@597
|
1179 |
Vector.this.set(lastRet, e);
|
jaroslav@597
|
1180 |
}
|
jaroslav@597
|
1181 |
}
|
jaroslav@597
|
1182 |
|
jaroslav@597
|
1183 |
public void add(E e) {
|
jaroslav@597
|
1184 |
int i = cursor;
|
jaroslav@597
|
1185 |
synchronized (Vector.this) {
|
jaroslav@597
|
1186 |
checkForComodification();
|
jaroslav@597
|
1187 |
Vector.this.add(i, e);
|
jaroslav@597
|
1188 |
expectedModCount = modCount;
|
jaroslav@597
|
1189 |
}
|
jaroslav@597
|
1190 |
cursor = i + 1;
|
jaroslav@597
|
1191 |
lastRet = -1;
|
jaroslav@597
|
1192 |
}
|
jaroslav@597
|
1193 |
}
|
jaroslav@597
|
1194 |
}
|