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