1 |
/* Vector.java -- Class that provides growable arrays. |
/* Vector.java -- Class that provides growable arrays. |
2 |
Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
30 |
import java.io.Serializable; |
import java.io.Serializable; |
31 |
|
|
32 |
/** |
/** |
33 |
* the <b>Vector</b> classes implements growable arrays of Objects. |
* The <code>Vector</code> classes implements growable arrays of Objects. |
34 |
* You can access elements in a Vector with an index, just as you |
* You can access elements in a Vector with an index, just as you |
35 |
* can in a built in array, but Vectors can grow and shrink to accomodate |
* can in a built in array, but Vectors can grow and shrink to accomodate |
36 |
* more or fewer objects. |
* more or fewer objects.<p> |
37 |
* |
* |
38 |
* Vectors try to mantain efficiency in growing by having a |
* Vectors try to mantain efficiency in growing by having a |
39 |
* <b>capacityIncrement</b> that can be specified at instantiation. |
* <code>capacityIncrement</code> that can be specified at instantiation. |
40 |
* When a Vector can no longer hold a new Object, it grows by the amount |
* When a Vector can no longer hold a new Object, it grows by the amount |
41 |
* in <b>capacityIncrement</b>. |
* in <code>capacityIncrement</code>. If this value is 0, the vector doubles in |
42 |
|
* size.<p> |
43 |
* |
* |
44 |
* Vector implements the JDK 1.2 List interface, and is therefor a fully |
* Vector implements the JDK 1.2 List interface, and is therefore a fully |
45 |
* compliant Collection object. |
* compliant Collection object. The iterators are fail-fast - if external |
46 |
|
* code structurally modifies the vector, any operation on the iterator will |
47 |
|
* then throw a {@link ConcurrentModificationException}. The Vector class is |
48 |
|
* fully synchronized, but the iterators are not. So, when iterating over a |
49 |
|
* vector, be sure to synchronize on the vector itself. If you don't want the |
50 |
|
* expense of synchronization, use ArrayList instead. On the other hand, the |
51 |
|
* Enumeration of elements() is not thread-safe, nor is it fail-fast; so it |
52 |
|
* can lead to undefined behavior even in a single thread if you modify the |
53 |
|
* vector during iteration.<p> |
54 |
|
* |
55 |
|
* Note: Some methods, especially those specified by List, specify throwing |
56 |
|
* {@link IndexOutOfBoundsException}, but it is easier to implement by |
57 |
|
* throwing the subclass {@link ArrayIndexOutOfBoundsException}. Others |
58 |
|
* directly specify this subclass. |
59 |
* |
* |
|
* @specnote The JCL claims that various methods in this class throw |
|
|
* IndexOutOfBoundsException, which would be consistent with other collections |
|
|
* classes. ArrayIndexOutOfBoundsException is actually thrown, per the online |
|
|
* docs, even for List method implementations. |
|
|
* |
|
60 |
* @author Scott G. Miller |
* @author Scott G. Miller |
61 |
|
* @author Eric Blake <ebb9@email.byu.edu> |
62 |
|
* @see Collection |
63 |
|
* @see List |
64 |
|
* @see ArrayList |
65 |
|
* @see LinkedList |
66 |
|
* @since 1.0 |
67 |
|
* @status updated to 1.4 |
68 |
*/ |
*/ |
69 |
public class Vector extends AbstractList |
public class Vector extends AbstractList |
70 |
implements List, Cloneable, Serializable |
implements List, RandomAccess, Cloneable, Serializable |
71 |
{ |
{ |
72 |
/** |
/** |
73 |
* The amount the Vector's internal array should be increased in size when |
* Compatible with JDK 1.0+. |
74 |
* a new element is added that exceeds the current size of the array, |
*/ |
75 |
* or when {@link #ensureCapacity} is called. |
private static final long serialVersionUID = -2767605614048989439L; |
76 |
* @serial |
|
77 |
|
/** |
78 |
|
* The internal array used to hold members of a Vector. The elements are |
79 |
|
* in positions 0 through elementCount - 1, and all remaining slots are null. |
80 |
|
* @serial the elements |
81 |
*/ |
*/ |
82 |
protected int capacityIncrement = 0; |
protected Object[] elementData; |
83 |
|
|
84 |
/** |
/** |
85 |
* The number of elements currently in the vector, also returned by |
* The number of elements currently in the vector, also returned by |
86 |
* {@link #size}. |
* {@link #size}. |
87 |
* @serial |
* @serial the size |
88 |
*/ |
*/ |
89 |
protected int elementCount = 0; |
protected int elementCount; |
90 |
|
|
91 |
/** |
/** |
92 |
* The internal array used to hold members of a Vector |
* The amount the Vector's internal array should be increased in size when |
93 |
* @serial |
* a new element is added that exceeds the current size of the array, |
94 |
|
* or when {@link #ensureCapacity} is called. If <= 0, the vector just |
95 |
|
* doubles in size. |
96 |
|
* @serial the amount to grow the vector by |
97 |
*/ |
*/ |
98 |
protected Object[] elementData; |
protected int capacityIncrement; |
|
|
|
|
private static final long serialVersionUID = -2767605614048989439L; |
|
99 |
|
|
100 |
/** |
/** |
101 |
* Constructs an empty vector with an initial size of 10, and |
* Constructs an empty vector with an initial size of 10, and |
103 |
*/ |
*/ |
104 |
public Vector() |
public Vector() |
105 |
{ |
{ |
106 |
this(10); |
this(10, 0); |
107 |
} |
} |
108 |
|
|
109 |
/** |
/** |
110 |
* Constructs a vector containing the contents of Collection, in the |
* Constructs a vector containing the contents of Collection, in the |
111 |
* order given by the collection |
* order given by the collection. |
112 |
* |
* |
113 |
* @param c A collection of elements to be added to the newly constructed |
* @param c collection of elements to add to the new vector |
114 |
* vector |
* @throws NullPointerException if c is null |
115 |
|
* @since 1.2 |
116 |
*/ |
*/ |
117 |
public Vector(Collection c) |
public Vector(Collection c) |
118 |
{ |
{ |
119 |
int csize = c.size(); |
elementCount = c.size(); |
120 |
elementData = new Object[csize]; |
elementData = c.toArray(new Object[elementCount]); |
|
elementCount = csize; |
|
|
Iterator itr = c.iterator(); |
|
|
for (int i = 0; i < csize; i++) |
|
|
{ |
|
|
elementData[i] = itr.next(); |
|
|
} |
|
121 |
} |
} |
122 |
|
|
123 |
/** |
/** |
124 |
* Constructs a Vector with the initial capacity and capacity |
* Constructs a Vector with the initial capacity and capacity |
125 |
* increment specified |
* increment specified. |
126 |
* |
* |
127 |
* @param initialCapacity The initial size of the Vector's internal |
* @param initialCapacity the initial size of the Vector's internal array |
128 |
* array |
* @param capacityIncrement the amount the internal array should be |
129 |
* @param capacityIncrement The amount the internal array should be |
* increased by when necessary, 0 to double the size |
130 |
* increased if necessary |
* @throws IllegalArgumentException if initialCapacity < 0 |
131 |
*/ |
*/ |
132 |
public Vector(int initialCapacity, int capacityIncrement) |
public Vector(int initialCapacity, int capacityIncrement) |
133 |
{ |
{ |
138 |
} |
} |
139 |
|
|
140 |
/** |
/** |
141 |
* Constructs a Vector with the initial capacity specified |
* Constructs a Vector with the initial capacity specified, and a capacity |
142 |
|
* increment of 0 (double in size). |
143 |
* |
* |
144 |
* @param initialCapacity The initial size of the Vector's internal array |
* @param initialCapacity the initial size of the Vector's internal array |
145 |
|
* @throws IllegalArgumentException if initialCapacity < 0 |
146 |
*/ |
*/ |
147 |
public Vector(int initialCapacity) |
public Vector(int initialCapacity) |
148 |
{ |
{ |
149 |
if (initialCapacity < 0) |
this(initialCapacity, 0); |
|
throw new IllegalArgumentException(); |
|
|
elementData = new Object[initialCapacity]; |
|
150 |
} |
} |
151 |
|
|
152 |
/** |
/** |
153 |
* Copies the contents of a provided array into the Vector. If the |
* Copies the contents of a provided array into the Vector. If the |
154 |
* array is too large to fit in the Vector, an ArrayIndexOutOfBoundsException |
* array is too large to fit in the Vector, an IndexOutOfBoundsException |
155 |
* is thrown. Old elements in the Vector are overwritten by the new |
* is thrown without modifying the array. Old elements in the Vector are |
156 |
* elements |
* overwritten by the new elements. |
157 |
* |
* |
158 |
* @param anArray An array from which elements will be copied into the Vector |
* @param a target array for the copy |
159 |
* |
* @throws IndexOutOfBoundsException the array is not large enough |
160 |
* @throws ArrayIndexOutOfBoundsException the array being copied |
* @throws NullPointerException the array is null |
161 |
* is larger than the Vectors internal data array |
* @see #toArray(Object[]) |
162 |
*/ |
*/ |
163 |
public synchronized void copyInto(Object[] anArray) |
public synchronized void copyInto(Object[] a) |
164 |
{ |
{ |
165 |
System.arraycopy(elementData, 0, anArray, 0, elementCount); |
System.arraycopy(elementData, 0, a, 0, elementCount); |
166 |
} |
} |
167 |
|
|
168 |
/** |
/** |
169 |
* Trims the Vector down to size. If the internal data array is larger |
* Trims the Vector down to size. If the internal data array is larger |
170 |
* than the number of Objects its holding, a new array is constructed |
* than the number of Objects its holding, a new array is constructed |
171 |
* that precisely holds the elements. |
* that precisely holds the elements. Otherwise this does nothing. |
172 |
*/ |
*/ |
173 |
public synchronized void trimToSize() |
public synchronized void trimToSize() |
174 |
{ |
{ |
182 |
} |
} |
183 |
|
|
184 |
/** |
/** |
185 |
* Ensures that <b>minCapacity</b> elements can fit within this Vector. |
* Ensures that <code>minCapacity</code> elements can fit within this Vector. |
186 |
* If it cannot hold this many elements, the internal data array is expanded |
* If <code>elementData</code> is too small, it is expanded as follows: |
187 |
* in the following manner. If the current size plus the capacityIncrement |
* If the <code>elementCount + capacityIncrement</code> is adequate, that |
188 |
* is sufficient, the internal array is expanded by capacityIncrement. |
* is the new size. If <code>capacityIncrement</code> is non-zero, the |
189 |
* If capacityIncrement is non-positive, the size is doubled. If |
* candidate size is double the current. If that is not enough, the new |
190 |
* neither is sufficient, the internal array is expanded to size minCapacity |
* size is <code>minCapacity</code>. |
191 |
* |
* |
192 |
* @param minCapacity The minimum capacity the internal array should be |
* @param minCapacity the desired minimum capacity, negative values ignored |
|
* able to handle after executing this method |
|
193 |
*/ |
*/ |
194 |
public synchronized void ensureCapacity(int minCapacity) |
public synchronized void ensureCapacity(int minCapacity) |
195 |
{ |
{ |
196 |
if (elementData.length >= minCapacity) |
if (elementData.length >= minCapacity) |
197 |
return; |
return; |
198 |
|
|
199 |
int newCapacity; |
int newCapacity; |
200 |
if (capacityIncrement <= 0) |
if (capacityIncrement <= 0) |
201 |
newCapacity = elementData.length * 2; |
newCapacity = elementData.length * 2; |
202 |
else |
else |
203 |
newCapacity = elementData.length + capacityIncrement; |
newCapacity = elementData.length + capacityIncrement; |
204 |
|
|
205 |
Object[] newArray = new Object[Math.max(newCapacity, minCapacity)]; |
Object[] newArray = new Object[Math.max(newCapacity, minCapacity)]; |
206 |
|
|
207 |
System.arraycopy(elementData, 0, newArray, 0, elementData.length); |
System.arraycopy(elementData, 0, newArray, 0, elementCount); |
208 |
elementData = newArray; |
elementData = newArray; |
209 |
} |
} |
210 |
|
|
211 |
/** |
/** |
212 |
* Explicitly sets the size of the internal data array, copying the |
* Explicitly sets the size of the vector (but not necessarily the size of |
213 |
* old values to the new internal array. If the new array is smaller |
* the internal data array). If the new size is smaller than the old one, |
214 |
* than the old one, old values that don't fit are lost. If the new size |
* old values that don't fit are lost. If the new size is larger than the |
215 |
* is larger than the old one, the vector is padded with null entries. |
* old one, the vector is padded with null entries. |
216 |
* |
* |
217 |
* @param newSize The new size of the internal array |
* @param newSize The new size of the internal array |
218 |
|
* @throws ArrayIndexOutOfBoundsException if the new size is negative |
219 |
*/ |
*/ |
220 |
public synchronized void setSize(int newSize) |
public synchronized void setSize(int newSize) |
221 |
{ |
{ |
222 |
|
// Don't bother checking for the case where size() == the capacity of the |
223 |
|
// vector since that is a much less likely case; it's more efficient to |
224 |
|
// not do the check and lose a bit of performance in that infrequent case |
225 |
modCount++; |
modCount++; |
226 |
Object[] newArray = new Object[newSize]; |
ensureCapacity(newSize); |
227 |
System.arraycopy(elementData, 0, newArray, 0, |
if (newSize < elementCount) |
228 |
Math.min(newSize, elementCount)); |
Arrays.fill(elementData, newSize, elementCount, null); |
229 |
elementCount = newSize; |
elementCount = newSize; |
|
elementData = newArray; |
|
230 |
} |
} |
231 |
|
|
232 |
/** |
/** |
233 |
* Returns the size of the internal data array (not the amount of elements |
* Returns the size of the internal data array (not the amount of elements |
234 |
* contained in the Vector) |
* contained in the Vector). |
235 |
* |
* |
236 |
* @returns capacity of the internal data array |
* @return capacity of the internal data array |
237 |
*/ |
*/ |
238 |
public int capacity() |
public synchronized int capacity() |
239 |
{ |
{ |
240 |
return elementData.length; |
return elementData.length; |
241 |
} |
} |
242 |
|
|
243 |
/** |
/** |
244 |
* Returns the number of elements stored in this Vector |
* Returns the number of elements stored in this Vector. |
245 |
* |
* |
246 |
* @returns the number of elements in this Vector |
* @return the number of elements in this Vector |
247 |
*/ |
*/ |
248 |
public int size() |
public synchronized int size() |
249 |
{ |
{ |
250 |
return elementCount; |
return elementCount; |
251 |
} |
} |
253 |
/** |
/** |
254 |
* Returns true if this Vector is empty, false otherwise |
* Returns true if this Vector is empty, false otherwise |
255 |
* |
* |
256 |
* @returns true if the Vector is empty, false otherwise |
* @return true if the Vector is empty, false otherwise |
257 |
*/ |
*/ |
258 |
public boolean isEmpty() |
public synchronized boolean isEmpty() |
259 |
{ |
{ |
260 |
return elementCount == 0; |
return elementCount == 0; |
261 |
} |
} |
262 |
|
|
263 |
/** |
/** |
264 |
* Searches the vector starting at <b>index</b> for object <b>elem</b> |
* Returns an Enumeration of the elements of this Vector. The enumeration |
265 |
* and returns the index of the first occurence of this Object. If |
* visits the elements in increasing index order, but is NOT thread-safe. |
266 |
* the object is not found, -1 is returned |
* |
267 |
* |
* @return an Enumeration |
268 |
* @param e The Object to search for |
* @see #iterator() |
|
* @param index Start searching at this index |
|
|
* @returns The index of the first occurence of <b>elem</b>, or -1 |
|
|
* if it is not found |
|
269 |
*/ |
*/ |
270 |
public synchronized int indexOf(Object e, int index) |
// No need to synchronize as the Enumeration is not thread-safe! |
271 |
|
public Enumeration elements() |
272 |
{ |
{ |
273 |
for (int i = index; i < elementCount; i++) |
return new Enumeration() |
274 |
|
{ |
275 |
|
private int i = 0; |
276 |
|
|
277 |
|
public boolean hasMoreElements() |
278 |
{ |
{ |
279 |
if (e == null ? elementData[i] == null : e.equals(elementData[i])) |
return i < elementCount; |
|
return i; |
|
280 |
} |
} |
281 |
return -1; |
|
282 |
|
public Object nextElement() |
283 |
|
{ |
284 |
|
if (i >= elementCount) |
285 |
|
throw new NoSuchElementException(); |
286 |
|
return elementData[i++]; |
287 |
|
} |
288 |
|
}; |
289 |
} |
} |
290 |
|
|
291 |
/** |
/** |
292 |
* Returns the first occurence of <b>elem</b> in the Vector, or -1 if |
* Returns true when <code>elem</code> is contained in this Vector. |
|
* <b>elem</b> is not found. |
|
293 |
* |
* |
294 |
* @param elem The object to search for |
* @param elem the element to check |
295 |
* @returns The index of the first occurence of <b>elem</b> or -1 if |
* @return true if the object is contained in this Vector, false otherwise |
|
* not found |
|
296 |
*/ |
*/ |
297 |
public int indexOf(Object elem) |
public boolean contains(Object elem) |
298 |
{ |
{ |
299 |
return indexOf(elem, 0); |
return indexOf(elem, 0) >= 0; |
300 |
} |
} |
301 |
|
|
302 |
/** |
/** |
303 |
* Returns true if <b>elem</b> is contained in this Vector, false otherwise. |
* Returns the first occurence of <code>elem</code> in the Vector, or -1 if |
304 |
|
* <code>elem</code> is not found. |
305 |
* |
* |
306 |
* @param elem The element to check |
* @param elem the object to search for |
307 |
* @returns true if the object is contained in this Vector, false otherwise |
* @return the index of the first occurence, or -1 if not found |
308 |
*/ |
*/ |
309 |
public boolean contains(Object elem) |
public int indexOf(Object elem) |
310 |
{ |
{ |
311 |
return indexOf(elem, 0) != -1; |
return indexOf(elem, 0); |
312 |
} |
} |
313 |
|
|
314 |
/** |
/** |
315 |
* Returns the index of the first occurence of <b>elem</b>, when searching |
* Searches the vector starting at <code>index</code> for object |
316 |
* backwards from <b>index</b>. If the object does not occur in this Vector, |
* <code>elem</code> and returns the index of the first occurence of this |
317 |
* -1 is returned. |
* Object. If the object is not found, or index is larger than the size |
318 |
* |
* of the vector, -1 is returned. |
319 |
* @param eThe object to search for |
* |
320 |
* @param index The index to start searching in reverse from |
* @param e the Object to search for |
321 |
* @returns The index of the Object if found, -1 otherwise |
* @param index start searching at this index |
322 |
|
* @return the index of the next occurence, or -1 if it is not found |
323 |
|
* @throws IndexOutOfBoundsException if index < 0 |
324 |
*/ |
*/ |
325 |
public synchronized int lastIndexOf(Object e, int index) |
public synchronized int indexOf(Object e, int index) |
326 |
{ |
{ |
327 |
if (index >= elementCount) |
for (int i = index; i < elementCount; i++) |
328 |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
if (equals(e, elementData[i])) |
329 |
|
return i; |
|
for (int i = index; i >= 0; i--) |
|
|
{ |
|
|
if (e == null ? elementData[i] == null : e.equals(elementData[i])) |
|
|
return i; |
|
|
} |
|
330 |
return -1; |
return -1; |
331 |
} |
} |
332 |
|
|
333 |
/** |
/** |
334 |
* Returns the last index of <b>elem</b> within this Vector, or -1 |
* Returns the last index of <code>elem</code> within this Vector, or -1 |
335 |
* if the object is not within the Vector |
* if the object is not within the Vector. |
336 |
* |
* |
337 |
* @param elem The object to search for |
* @param elem the object to search for |
338 |
* @returns the last index of the object, or -1 if not found |
* @return the last index of the object, or -1 if not found |
339 |
*/ |
*/ |
340 |
public int lastIndexOf(Object elem) |
public int lastIndexOf(Object elem) |
341 |
{ |
{ |
343 |
} |
} |
344 |
|
|
345 |
/** |
/** |
346 |
* Returns the Object stored at <b>index</b>. If index is out of range |
* Returns the index of the first occurence of <code>elem</code>, when |
347 |
* an ArrayIndexOutOfBoundsException is thrown. |
* searching backwards from <code>index</code>. If the object does not |
348 |
|
* occur in this Vector, or index is less than 0, -1 is returned. |
349 |
|
* |
350 |
|
* @param e the object to search for |
351 |
|
* @param index the index to start searching in reverse from |
352 |
|
* @return the index of the Object if found, -1 otherwise |
353 |
|
* @throws IndexOutOfBoundsException if index >= size() |
354 |
|
*/ |
355 |
|
public synchronized int lastIndexOf(Object e, int index) |
356 |
|
{ |
357 |
|
if (index >= elementCount) |
358 |
|
throw new IndexOutOfBoundsException(index + " >= " + elementCount); |
359 |
|
|
360 |
|
for (int i = index; i >= 0; i--) |
361 |
|
if (equals(e, elementData[i])) |
362 |
|
return i; |
363 |
|
return -1; |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
|
* Returns the Object stored at <code>index</code>. |
368 |
* |
* |
369 |
* @param index the index of the Object to retrieve |
* @param index the index of the Object to retrieve |
370 |
* @returns The object at <b>index</b> |
* @return the object at <code>index</code> |
371 |
* @throws ArrayIndexOutOfBoundsException <b>index</b> is |
* @throws ArrayIndexOutOfBoundsException index < 0 || index >= size() |
372 |
* larger than the Vector |
* @see #get(int) |
373 |
*/ |
*/ |
374 |
public synchronized Object elementAt(int index) |
public synchronized Object elementAt(int index) |
375 |
{ |
{ |
376 |
//Within the bounds of this Vector does not necessarily mean within |
// Within the bounds of this Vector does not necessarily mean within |
377 |
//the bounds of the internal array |
// the bounds of the internal array. |
378 |
if (index >= elementCount) |
if (index >= elementCount) |
379 |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
380 |
|
|
382 |
} |
} |
383 |
|
|
384 |
/** |
/** |
385 |
* Returns the first element in the Vector. If there is no first Object |
* Returns the first element (index 0) in the Vector. |
|
* (The vector is empty), a NoSuchElementException is thrown. |
|
386 |
* |
* |
387 |
* @returns The first Object in the Vector |
* @return the first Object in the Vector |
388 |
* @throws NoSuchElementException the Vector is empty |
* @throws NoSuchElementException the Vector is empty |
389 |
*/ |
*/ |
390 |
public synchronized Object firstElement() |
public synchronized Object firstElement() |
392 |
if (elementCount == 0) |
if (elementCount == 0) |
393 |
throw new NoSuchElementException(); |
throw new NoSuchElementException(); |
394 |
|
|
395 |
return elementAt(0); |
return elementData[0]; |
396 |
} |
} |
397 |
|
|
398 |
/** |
/** |
399 |
* Returns the last element in the Vector. If the Vector has no last element |
* Returns the last element in the Vector. |
|
* (The vector is empty), a NoSuchElementException is thrown. |
|
400 |
* |
* |
401 |
* @returns The last Object in the Vector |
* @return the last Object in the Vector |
402 |
* @throws NoSuchElementException the Vector is empty |
* @throws NoSuchElementException the Vector is empty |
403 |
*/ |
*/ |
404 |
public synchronized Object lastElement() |
public synchronized Object lastElement() |
406 |
if (elementCount == 0) |
if (elementCount == 0) |
407 |
throw new NoSuchElementException(); |
throw new NoSuchElementException(); |
408 |
|
|
409 |
return elementAt(elementCount - 1); |
return elementData[elementCount - 1]; |
|
} |
|
|
|
|
|
/** |
|
|
* Places <b>obj</b> at <b>index</b> within the Vector. If <b>index</b> |
|
|
* refers to an index outside the Vector, an ArrayIndexOutOfBoundsException |
|
|
* is thrown. |
|
|
* |
|
|
* @param obj The object to store |
|
|
* @param index The position in the Vector to store the object |
|
|
* @throws ArrayIndexOutOfBoundsException the index is out of range |
|
|
*/ |
|
|
public synchronized void setElementAt(Object obj, int index) |
|
|
{ |
|
|
if (index >= elementCount) |
|
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
|
|
|
|
|
elementData[index] = obj; |
|
410 |
} |
} |
411 |
|
|
412 |
/** |
/** |
413 |
* Puts <b>element</b> into the Vector at position <b>index</b> and returns |
* Changes the element at <code>index</code> to be <code>obj</code> |
|
* the Object that previously occupied that position. |
|
414 |
* |
* |
415 |
* @param index The index within the Vector to place the Object |
* @param obj the object to store |
416 |
* @param element The Object to store in the Vector |
* @param index the position in the Vector to store the object |
|
* @returns The previous object at the specified index |
|
417 |
* @throws ArrayIndexOutOfBoundsException the index is out of range |
* @throws ArrayIndexOutOfBoundsException the index is out of range |
418 |
* |
* @see #set(int, Object) |
419 |
*/ |
*/ |
420 |
public synchronized Object set(int index, Object element) |
public void setElementAt(Object obj, int index) |
421 |
{ |
{ |
422 |
if (index >= elementCount) |
set(index, obj); |
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
|
|
|
|
|
Object temp = elementData[index]; |
|
|
elementData[index] = element; |
|
|
return temp; |
|
423 |
} |
} |
424 |
|
|
425 |
/** |
/** |
426 |
* Removes the element at <b>index</b>, and shifts all elements at |
* Removes the element at <code>index</code>, and shifts all elements at |
427 |
* positions greater than index to their index - 1. |
* positions greater than index to their index - 1. |
428 |
* |
* |
429 |
* @param index The index of the element to remove |
* @param index the index of the element to remove |
430 |
|
* @throws ArrayIndexOutOfBoundsException index < 0 || index >= size(); |
431 |
|
* @see #remove(int) |
432 |
*/ |
*/ |
433 |
public synchronized void removeElementAt(int index) |
public void removeElementAt(int index) |
434 |
{ |
{ |
435 |
if (index >= elementCount) |
remove(index); |
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
|
|
|
|
|
modCount++; |
|
|
elementCount--; |
|
|
if (index < elementCount) |
|
|
System.arraycopy(elementData, index + 1, elementData, index, |
|
|
elementCount - index); |
|
|
//Delete the last element (which has been copied back one index) |
|
|
//so it can be garbage collected; |
|
|
elementData[elementCount] = null; |
|
436 |
} |
} |
437 |
|
|
438 |
/** |
/** |
439 |
* Inserts a new element into the Vector at <b>index</b>. Any elements |
* Inserts a new element into the Vector at <code>index</code>. Any elements |
440 |
* at or greater than index are shifted up one position. |
* at or greater than index are shifted up one position. |
441 |
* |
* |
442 |
* @param obj The object to insert |
* @param obj the object to insert |
443 |
* @param index The index at which the object is inserted |
* @param index the index at which the object is inserted |
444 |
|
* @throws ArrayIndexOutOfBoundsException index < 0 || index > size() |
445 |
|
* @see #add(int, Object) |
446 |
*/ |
*/ |
447 |
public void insertElementAt(Object obj, int index) |
public synchronized void insertElementAt(Object obj, int index) |
448 |
{ |
{ |
449 |
if (index > elementCount) |
if (index > elementCount) |
450 |
throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); |
throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); |
451 |
|
|
452 |
if (elementCount == elementData.length) |
if (elementCount == elementData.length) |
453 |
ensureCapacity(elementCount + 1); |
ensureCapacity(elementCount + 1); |
454 |
++modCount; |
modCount++; |
|
++elementCount; |
|
455 |
System.arraycopy(elementData, index, elementData, index + 1, |
System.arraycopy(elementData, index, elementData, index + 1, |
456 |
elementCount - 1 - index); |
elementCount - index); |
457 |
|
elementCount++; |
458 |
elementData[index] = obj; |
elementData[index] = obj; |
459 |
} |
} |
460 |
|
|
461 |
/** |
/** |
462 |
* Adds an element to the Vector at the end of the Vector. If the vector |
* Adds an element to the Vector at the end of the Vector. The vector |
463 |
* cannot hold the element with its present capacity, its size is increased |
* is increased by ensureCapacity(size() + 1) if needed. |
|
* based on the same rules followed if ensureCapacity was called with the |
|
|
* argument currentSize+1. |
|
464 |
* |
* |
465 |
* @param obj The object to add to the Vector |
* @param obj the object to add to the Vector |
466 |
*/ |
*/ |
467 |
public synchronized void addElement(Object obj) |
public synchronized void addElement(Object obj) |
468 |
{ |
{ |
473 |
} |
} |
474 |
|
|
475 |
/** |
/** |
476 |
* Removes the first occurence of the given object from the Vector. |
* Removes the first (the lowestindex) occurance of the given object from |
477 |
* If such a remove was performed (the object was found), true is returned. |
* the Vector. If such a remove was performed (the object was found), true |
478 |
* If there was no such object, false is returned. |
* is returned. If there was no such object, false is returned. |
479 |
* |
* |
480 |
* @param obj The object to remove from the Vector |
* @param obj the object to remove from the Vector |
481 |
* @returns true if the Object was in the Vector, false otherwise |
* @return true if the Object was in the Vector, false otherwise |
482 |
|
* @see #remove(Object) |
483 |
*/ |
*/ |
484 |
public synchronized boolean removeElement(Object obj) |
public synchronized boolean removeElement(Object obj) |
485 |
{ |
{ |
486 |
int idx = indexOf(obj); |
int idx = indexOf(obj, 0); |
487 |
if (idx != -1) |
if (idx >= 0) |
488 |
{ |
{ |
489 |
removeElementAt(idx); |
remove(idx); |
490 |
return true; |
return true; |
491 |
} |
} |
492 |
return false; |
return false; |
493 |
} |
} |
495 |
/** |
/** |
496 |
* Removes all elements from the Vector. Note that this does not |
* Removes all elements from the Vector. Note that this does not |
497 |
* resize the internal data array. |
* resize the internal data array. |
498 |
|
* |
499 |
|
* @see #clear() |
500 |
*/ |
*/ |
501 |
public synchronized void removeAllElements() |
public synchronized void removeAllElements() |
502 |
{ |
{ |
|
modCount++; |
|
503 |
if (elementCount == 0) |
if (elementCount == 0) |
504 |
return; |
return; |
505 |
|
|
506 |
for (int i = elementCount - 1; i >= 0; --i) |
modCount++; |
507 |
{ |
Arrays.fill(elementData, 0, elementCount, null); |
|
elementData[i] = null; |
|
|
} |
|
508 |
elementCount = 0; |
elementCount = 0; |
509 |
} |
} |
510 |
|
|
511 |
/** |
/** |
512 |
* Creates a new Vector with the same contents as this one. |
* Creates a new Vector with the same contents as this one. The clone is |
513 |
|
* shallow; elements are not cloned. |
514 |
|
* |
515 |
|
* @return the clone of this vector |
516 |
*/ |
*/ |
517 |
public synchronized Object clone() |
public synchronized Object clone() |
518 |
{ |
{ |
519 |
try |
try |
520 |
{ |
{ |
521 |
Vector clone = (Vector) super.clone(); |
Vector clone = (Vector) super.clone(); |
522 |
clone.elementData = (Object[]) elementData.clone(); |
clone.elementData = (Object[]) elementData.clone(); |
523 |
return clone; |
return clone; |
524 |
} |
} |
525 |
catch (CloneNotSupportedException ex) |
catch (CloneNotSupportedException ex) |
526 |
{ |
{ |
527 |
throw new InternalError(ex.toString()); |
// Impossible to get here. |
528 |
|
throw new InternalError(ex.toString()); |
529 |
} |
} |
530 |
} |
} |
531 |
|
|
532 |
/** |
/** |
533 |
* Returns an Object array with the contents of this Vector, in the order |
* Returns an Object array with the contents of this Vector, in the order |
534 |
* they are stored within this Vector. Note that the Object array returned |
* they are stored within this Vector. Note that the Object array returned |
535 |
* is not the internal data array, and that it holds only the elements |
* is not the internal data array, and that it holds only the elements |
536 |
* within the Vector. This is similar to creating a new Object[] with the |
* within the Vector. This is similar to creating a new Object[] with the |
537 |
* size of this Vector, then calling Vector.copyInto(yourArray). |
* size of this Vector, then calling Vector.copyInto(yourArray). |
538 |
* |
* |
539 |
* @returns An Object[] containing the contents of this Vector in order |
* @return an Object[] containing the contents of this Vector in order |
540 |
* |
* @since 1.2 |
541 |
*/ |
*/ |
542 |
public synchronized Object[] toArray() |
public synchronized Object[] toArray() |
543 |
{ |
{ |
547 |
} |
} |
548 |
|
|
549 |
/** |
/** |
550 |
* Returns an array containing the contents of this Vector. |
* Returns an array containing the contents of this Vector. |
551 |
* If the provided array is large enough, the contents are copied |
* If the provided array is large enough, the contents are copied |
552 |
* into that array, and a null is placed in the position size(). |
* into that array, and a null is placed in the position size(). |
553 |
* In this manner, you can obtain the size of a Vector by the position |
* In this manner, you can obtain the size of a Vector by the position |
554 |
* of the null element. If the type of the provided array cannot |
* of the null element, if you know the vector does not itself contain |
555 |
* hold the elements, an ArrayStoreException is thrown. |
* null entries. If the array is not large enough, reflection is used |
556 |
* |
* to create a bigger one of the same runtime type. |
|
* If the provided array is not large enough, |
|
|
* a new one is created with the contents of the Vector, and no null |
|
|
* element. The new array is of the same runtime type as the provided |
|
|
* array. |
|
|
* |
|
557 |
* |
* |
558 |
* @param array An array to copy the Vector into if large enough |
* @param a an array to copy the Vector into if large enough |
559 |
* @returns An array with the contents of this Vector in order |
* @return an array with the contents of this Vector in order |
560 |
* @throws ArrayStoreException the runtime type of the provided array |
* @throws ArrayStoreException the runtime type of the provided array |
561 |
* cannot hold the elements of the Vector |
* cannot hold the elements of the Vector |
562 |
|
* @throws NullPointerException if <code>a</code> is null |
563 |
|
* @since 1.2 |
564 |
*/ |
*/ |
565 |
public synchronized Object[] toArray(Object[] array) |
public synchronized Object[] toArray(Object[] a) |
566 |
{ |
{ |
567 |
if (array.length < elementCount) |
if (a.length < elementCount) |
568 |
array = (Object[]) Array.newInstance(array.getClass().getComponentType(), |
a = (Object[]) Array.newInstance(a.getClass().getComponentType(), |
569 |
elementCount); |
elementCount); |
570 |
else if (array.length > elementCount) |
else if (a.length > elementCount) |
571 |
array[elementCount] = null; |
a[elementCount] = null; |
572 |
System.arraycopy(elementData, 0, array, 0, elementCount); |
System.arraycopy(elementData, 0, a, 0, elementCount); |
573 |
return array; |
return a; |
574 |
} |
} |
575 |
|
|
576 |
/** |
/** |
577 |
* Returns the element at position <b>index</b> |
* Returns the element at position <code>index</code>. |
578 |
* |
* |
579 |
* @param index the position from which an element will be retrieved |
* @param index the position from which an element will be retrieved |
580 |
* @throws ArrayIndexOutOfBoundsException the index is not within the |
* @return the element at that position |
581 |
* range of the Vector |
* @throws ArrayIndexOutOfBoundsException index < 0 || index >= size() |
582 |
|
* @since 1.2 |
583 |
*/ |
*/ |
584 |
public synchronized Object get(int index) |
public Object get(int index) |
585 |
{ |
{ |
586 |
return elementAt(index); |
return elementAt(index); |
587 |
} |
} |
588 |
|
|
589 |
/** |
/** |
590 |
* Removes the given Object from the Vector. If it exists, true |
* Puts <code>element</code> into the Vector at position <code>index</code> |
591 |
* is returned, if not, false is returned. |
* and returns the Object that previously occupied that position. |
592 |
* |
* |
593 |
* @param o The object to remove from the Vector |
* @param index the index within the Vector to place the Object |
594 |
* @returns true if the Object existed in the Vector, false otherwise |
* @param element the Object to store in the Vector |
595 |
|
* @return the previous object at the specified index |
596 |
|
* @throws ArrayIndexOutOfBoundsException index < 0 || index >= size() |
597 |
|
* @since 1.2 |
598 |
*/ |
*/ |
599 |
public boolean remove(Object o) |
public synchronized Object set(int index, Object element) |
600 |
{ |
{ |
601 |
return removeElement(o); |
if (index >= elementCount) |
602 |
|
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
603 |
|
|
604 |
|
Object temp = elementData[index]; |
605 |
|
elementData[index] = element; |
606 |
|
return temp; |
607 |
} |
} |
608 |
|
|
609 |
/** |
/** |
610 |
* Adds an object to the Vector. |
* Adds an object to the Vector. |
611 |
* |
* |
612 |
* @param o The element to add to the Vector |
* @param o the element to add to the Vector |
613 |
|
* @return true, as specified by List |
614 |
|
* @since 1.2 |
615 |
*/ |
*/ |
616 |
public synchronized boolean add(Object o) |
public boolean add(Object o) |
617 |
{ |
{ |
618 |
addElement(o); |
addElement(o); |
619 |
return true; |
return true; |
620 |
} |
} |
621 |
|
|
622 |
/** |
/** |
623 |
|
* Removes the given Object from the Vector. If it exists, true |
624 |
|
* is returned, if not, false is returned. |
625 |
|
* |
626 |
|
* @param o the object to remove from the Vector |
627 |
|
* @return true if the Object existed in the Vector, false otherwise |
628 |
|
* @since 1.2 |
629 |
|
*/ |
630 |
|
public boolean remove(Object o) |
631 |
|
{ |
632 |
|
return removeElement(o); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
* Adds an object at the specified index. Elements at or above |
* Adds an object at the specified index. Elements at or above |
637 |
* index are shifted up one position. |
* index are shifted up one position. |
638 |
* |
* |
639 |
* @param index The index at which to add the element |
* @param index the index at which to add the element |
640 |
* @param element The element to add to the Vector |
* @param element the element to add to the Vector |
641 |
|
* @throws ArrayIndexOutOfBoundsException index < 0 || index > size() |
642 |
|
* @since 1.2 |
643 |
*/ |
*/ |
644 |
public void add(int index, Object element) |
public void add(int index, Object element) |
645 |
{ |
{ |
649 |
/** |
/** |
650 |
* Removes the element at the specified index, and returns it. |
* Removes the element at the specified index, and returns it. |
651 |
* |
* |
652 |
* @param index The position from which to remove the element |
* @param index the position from which to remove the element |
653 |
* @returns The object removed |
* @return the object removed |
654 |
* @throws ArrayIndexOutOfBoundsException the index was out of the range |
* @throws ArrayIndexOutOfBoundsException index < 0 || index >= size() |
655 |
* of the Vector |
* @since 1.2 |
656 |
*/ |
*/ |
657 |
public synchronized Object remove(int index) |
public synchronized Object remove(int index) |
658 |
{ |
{ |
659 |
if (index >= elementCount) |
if (index >= elementCount) |
660 |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); |
661 |
|
|
662 |
Object temp = elementData[index]; |
Object temp = elementData[index]; |
663 |
removeElementAt(index); |
modCount++; |
664 |
|
elementCount--; |
665 |
|
if (index < elementCount) |
666 |
|
System.arraycopy(elementData, index + 1, elementData, index, |
667 |
|
elementCount - index); |
668 |
|
elementData[elementCount] = null; |
669 |
return temp; |
return temp; |
670 |
} |
} |
671 |
|
|
672 |
/** |
/** |
673 |
* Clears all elements in the Vector and sets its size to 0 |
* Clears all elements in the Vector and sets its size to 0. |
674 |
*/ |
*/ |
675 |
public void clear() |
public void clear() |
676 |
{ |
{ |
677 |
removeAllElements(); |
removeAllElements(); |
678 |
} |
} |
679 |
|
|
680 |
|
/** |
681 |
|
* Returns true if this Vector contains all the elements in c. |
682 |
|
* |
683 |
|
* @param c the collection to compare to |
684 |
|
* @return true if this vector contains all elements of c |
685 |
|
* @throws NullPointerException if c is null |
686 |
|
* @since 1.2 |
687 |
|
*/ |
688 |
public synchronized boolean containsAll(Collection c) |
public synchronized boolean containsAll(Collection c) |
689 |
{ |
{ |
690 |
Iterator itr = c.iterator(); |
// Here just for the sychronization. |
691 |
int size = c.size(); |
return super.containsAll(c); |
|
for (int pos = 0; pos < size; pos++) |
|
|
{ |
|
|
if (!contains(itr.next())) |
|
|
return false; |
|
|
} |
|
|
return true; |
|
692 |
} |
} |
693 |
|
|
694 |
|
/** |
695 |
|
* Appends all elements of the given collection to the end of this Vector. |
696 |
|
* Behavior is undefined if the collection is modified during this operation |
697 |
|
* (for example, if this == c). |
698 |
|
* |
699 |
|
* @param c the collection to append |
700 |
|
* @return true if this vector changed, in other words c was not empty |
701 |
|
* @throws NullPointerException if c is null |
702 |
|
* @since 1.2 |
703 |
|
*/ |
704 |
public synchronized boolean addAll(Collection c) |
public synchronized boolean addAll(Collection c) |
705 |
{ |
{ |
706 |
return addAll(elementCount, c); |
return addAll(elementCount, c); |
707 |
} |
} |
708 |
|
|
709 |
|
/** |
710 |
|
* Remove from this vector all elements contained in the given collection. |
711 |
|
* |
712 |
|
* @param c the collection to filter out |
713 |
|
* @return true if this vector changed |
714 |
|
* @throws NullPointerException if c is null |
715 |
|
* @since 1.2 |
716 |
|
*/ |
717 |
public synchronized boolean removeAll(Collection c) |
public synchronized boolean removeAll(Collection c) |
718 |
{ |
{ |
719 |
return super.removeAll(c); |
int i; |
720 |
|
int j; |
721 |
|
for (i = 0; i < elementCount; i++) |
722 |
|
if (c.contains(elementData[i])) |
723 |
|
break; |
724 |
|
if (i == elementCount) |
725 |
|
return false; |
726 |
|
|
727 |
|
modCount++; |
728 |
|
for (j = i++; i < elementCount; i++) |
729 |
|
if (! c.contains(elementData[i])) |
730 |
|
elementData[j++] = elementData[i]; |
731 |
|
elementCount -= i - j; |
732 |
|
return true; |
733 |
} |
} |
734 |
|
|
735 |
|
/** |
736 |
|
* Retain in this vector only the elements contained in the given collection. |
737 |
|
* |
738 |
|
* @param c the collection to filter by |
739 |
|
* @return true if this vector changed |
740 |
|
* @throws NullPointerException if c is null |
741 |
|
* @since 1.2 |
742 |
|
*/ |
743 |
public synchronized boolean retainAll(Collection c) |
public synchronized boolean retainAll(Collection c) |
744 |
{ |
{ |
745 |
return super.retainAll(c); |
int i; |
746 |
|
int j; |
747 |
|
for (i = 0; i < elementCount; i++) |
748 |
|
if (! c.contains(elementData[i])) |
749 |
|
break; |
750 |
|
if (i == elementCount) |
751 |
|
return false; |
752 |
|
|
753 |
|
modCount++; |
754 |
|
for (j = i++; i < elementCount; i++) |
755 |
|
if (c.contains(elementData[i])) |
756 |
|
elementData[j++] = elementData[i]; |
757 |
|
elementCount -= i - j; |
758 |
|
return true; |
759 |
} |
} |
760 |
|
|
761 |
|
/** |
762 |
|
* Inserts all elements of the given collection at the given index of |
763 |
|
* this Vector. Behavior is undefined if the collection is modified during |
764 |
|
* this operation (for example, if this == c). |
765 |
|
* |
766 |
|
* @param c the collection to append |
767 |
|
* @return true if this vector changed, in other words c was not empty |
768 |
|
* @throws NullPointerException if c is null |
769 |
|
* @throws ArrayIndexOutOfBoundsException index < 0 || index > size() |
770 |
|
* @since 1.2 |
771 |
|
*/ |
772 |
public synchronized boolean addAll(int index, Collection c) |
public synchronized boolean addAll(int index, Collection c) |
773 |
{ |
{ |
774 |
if (index < 0 || index > elementCount) |
if (index > elementCount) |
775 |
throw new ArrayIndexOutOfBoundsException(index); |
throw new ArrayIndexOutOfBoundsException(index); |
|
modCount++; |
|
776 |
Iterator itr = c.iterator(); |
Iterator itr = c.iterator(); |
777 |
int csize = c.size(); |
int csize = c.size(); |
778 |
|
|
779 |
|
modCount++; |
780 |
ensureCapacity(elementCount + csize); |
ensureCapacity(elementCount + csize); |
781 |
int end = index + csize; |
int end = index + csize; |
782 |
if (elementCount > 0 && index != elementCount) |
if (elementCount > 0 && index != elementCount) |
783 |
System.arraycopy(elementData, index, elementData, end, csize); |
System.arraycopy(elementData, index, elementData, end, csize); |
784 |
elementCount += csize; |
elementCount += csize; |
785 |
for (; index < end; index++) |
for ( ; index < end; index++) |
786 |
{ |
elementData[index] = itr.next(); |
787 |
elementData[index] = itr.next(); |
return (csize > 0); |
|
} |
|
|
return (csize > 0); |
|
788 |
} |
} |
789 |
|
|
790 |
public synchronized boolean equals(Object c) |
/** |
791 |
|
* Compares this to the given object. |
792 |
|
* |
793 |
|
* @param o the object to compare to |
794 |
|
* @return true if the two are equal |
795 |
|
* @since 1.2 |
796 |
|
*/ |
797 |
|
public synchronized boolean equals(Object o) |
798 |
{ |
{ |
799 |
return super.equals(c); |
// Here just for the sychronization. |
800 |
|
return super.equals(o); |
801 |
} |
} |
802 |
|
|
803 |
|
/** |
804 |
|
* Computes the hashcode of this object. |
805 |
|
* |
806 |
|
* @return the hashcode |
807 |
|
* @since 1.2 |
808 |
|
*/ |
809 |
public synchronized int hashCode() |
public synchronized int hashCode() |
810 |
{ |
{ |
811 |
|
// Here just for the sychronization. |
812 |
return super.hashCode(); |
return super.hashCode(); |
813 |
} |
} |
814 |
|
|
815 |
/** |
/** |
816 |
* Returns a string representation of this Vector in the form |
* Returns a string representation of this Vector in the form |
817 |
* [element0, element1, ... elementN] |
* "[element0, element1, ... elementN]". |
818 |
* |
* |
819 |
* @returns the String representation of this Vector |
* @return the String representation of this Vector |
820 |
*/ |
*/ |
821 |
public synchronized String toString() |
public synchronized String toString() |
822 |
{ |
{ |
823 |
String r = "["; |
// Here just for the sychronization. |
824 |
for (int i = 0; i < elementCount; i++) |
return super.toString(); |
|
{ |
|
|
r += elementData[i]; |
|
|
if (i < elementCount - 1) |
|
|
r += ", "; |
|
|
} |
|
|
r += "]"; |
|
|
return r; |
|
825 |
} |
} |
826 |
|
|
827 |
/** |
/** |
828 |
* Returns an Enumeration of the elements of this List. |
* Obtain a List view of a subsection of this list, from fromIndex |
829 |
* The Enumeration returned is compatible behavior-wise with |
* (inclusive) to toIndex (exclusive). If the two indices are equal, the |
830 |
* the 1.1 elements() method, in that it does not check for |
* sublist is empty. The returned list is modifiable, and changes in one |
831 |
* concurrent modification. |
* reflect in the other. If this list is structurally modified in |
832 |
* |
* any way other than through the returned list, the result of any subsequent |
833 |
* @returns an Enumeration |
* operations on the returned list is undefined. |
834 |
|
* <p> |
835 |
|
* |
836 |
|
* @param fromIndex the index that the returned list should start from |
837 |
|
* (inclusive) |
838 |
|
* @param toIndex the index that the returned list should go to (exclusive) |
839 |
|
* @return a List backed by a subsection of this vector |
840 |
|
* @throws IndexOutOfBoundsException if fromIndex < 0 |
841 |
|
* || toIndex > size() |
842 |
|
* @throws IllegalArgumentException if fromIndex > toIndex |
843 |
|
* @see ConcurrentModificationException |
844 |
|
* @since 1.2 |
845 |
*/ |
*/ |
846 |
public synchronized Enumeration elements() |
public synchronized List subList(int fromIndex, int toIndex) |
|
{ |
|
|
return new Enumeration() |
|
|
{ |
|
|
int i = 0; |
|
|
public boolean hasMoreElements() |
|
|
{ |
|
|
return (i < elementCount); |
|
|
} |
|
|
public Object nextElement() |
|
|
{ |
|
|
if (i >= elementCount) |
|
|
throw new NoSuchElementException(); |
|
|
return (elementAt(i++)); |
|
|
} |
|
|
}; |
|
|
} |
|
|
|
|
|
public List subList(int fromIndex, int toIndex) |
|
847 |
{ |
{ |
848 |
List sub = super.subList(fromIndex, toIndex); |
List sub = super.subList(fromIndex, toIndex); |
849 |
return Collections.synchronizedList(sub); |
// We must specify the correct object to synchronize upon, hence the |
850 |
|
// use of a non-public API |
851 |
|
return new Collections.SynchronizedList(this, sub); |
852 |
} |
} |
853 |
|
|
854 |
/** @specnote This is not specified as synchronized in the JCL, but it seems |
/** |
855 |
* to me that is should be. If it isn't, a clear() operation on a sublist |
* Removes a range of elements from this list. |
856 |
* will not be synchronized w.r.t. the Vector object. |
* |
857 |
*/ |
* @param fromIndex the index to start deleting from (inclusive) |
858 |
protected synchronized void removeRange(int fromIndex, int toIndex) |
* @param toIndex the index to delete up to (exclusive) |
859 |
|
*/ |
860 |
|
// This does not need to be synchronized, because it is only called through |
861 |
|
// clear() of a sublist, and clear() had already synchronized. |
862 |
|
protected void removeRange(int fromIndex, int toIndex) |
863 |
{ |
{ |
|
modCount++; |
|
864 |
if (fromIndex != toIndex) |
if (fromIndex != toIndex) |
865 |
{ |
{ |
866 |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
modCount++; |
867 |
elementCount - toIndex); |
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
868 |
// Clear unused elements so objects can be collected. |
elementCount - toIndex); |
869 |
int save = elementCount; |
int save = elementCount; |
870 |
elementCount -= (toIndex - fromIndex); |
elementCount -= toIndex - fromIndex; |
871 |
for (int i = elementCount; i < save; ++i) |
Arrays.fill(elementData, elementCount, save, null); |
|
elementData[i] = null; |
|
872 |
} |
} |
873 |
} |
} |
874 |
} |
} |