ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Vector.java
Revision: 1.9
Committed: Sun Mar 19 17:59:39 2006 UTC (18 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.8: +10 -12 lines
Log Message:
sync with Mustang

File Contents

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