1 *java.util.Vector* *Vector* TheVectorclass implements a growable array of
5 extends |java.util.AbstractList|
6 implements |java.util.List|
7 |java.util.RandomAccess|
11 |java.util.Vector_Description|
12 |java.util.Vector_Fields|
13 |java.util.Vector_Constructors|
14 |java.util.Vector_Methods|
16 ================================================================================
18 *java.util.Vector_Fields*
19 |int_java.util.Vector.capacityIncrement|
20 |int_java.util.Vector.elementCount|
21 |java.lang.Object[]_java.util.Vector.elementData|
23 *java.util.Vector_Constructors*
24 |java.util.Vector()|Constructs an empty vector so that its internal data array
25 |java.util.Vector(Collection<?extendsE>)|Constructs a vector containing the ele
26 |java.util.Vector(int)|Constructs an empty vector with the specified initial ca
27 |java.util.Vector(int,int)|Constructs an empty vector with the specified initia
29 *java.util.Vector_Methods*
30 |java.util.Vector.add(E)|Appends the specified element to the end of this Vecto
31 |java.util.Vector.add(int,E)|Inserts the specified element at the specified pos
32 |java.util.Vector.addAll(Collection<?extendsE>)|Appends all of the elements in
33 |java.util.Vector.addAll(int,Collection<?extendsE>)|Inserts all of the elements
34 |java.util.Vector.addElement(E)|Adds the specified component to the end of this
35 |java.util.Vector.capacity()|Returns the current capacity of this vector.
36 |java.util.Vector.clear()|Removes all of the elements from this Vector.
37 |java.util.Vector.clone()|Returns a clone of this vector.
38 |java.util.Vector.contains(Object)|Returnstrueif this vector contains the speci
39 |java.util.Vector.containsAll(Collection<?>)|Returns true if this Vector contai
40 |java.util.Vector.copyInto(Object[])|Copies the components of this vector into
41 |java.util.Vector.elementAt(int)|Returns the component at the specified index.
42 |java.util.Vector.elements()|Returns an enumeration of the components of this v
43 |java.util.Vector.ensureCapacity(int)|Increases the capacity of this vector, if
44 |java.util.Vector.equals(Object)|Compares the specified Object with this Vector
45 |java.util.Vector.firstElement()|Returns the first component (the item at index
46 |java.util.Vector.get(int)|Returns the element at the specified position in thi
47 |java.util.Vector.hashCode()|Returns the hash code value for this Vector.
48 |java.util.Vector.indexOf(Object)|Returns the index of the first occurrence of
49 |java.util.Vector.indexOf(Object,int)|Returns the index of the first occurrence
50 |java.util.Vector.insertElementAt(E,int)|Inserts the specified object as a comp
51 |java.util.Vector.isEmpty()|Tests if this vector has no components.
52 |java.util.Vector.lastElement()|Returns the last component of the vector.
53 |java.util.Vector.lastIndexOf(Object)|Returns the index of the last occurrence
54 |java.util.Vector.lastIndexOf(Object,int)|Returns the index of the last occurre
55 |java.util.Vector.remove(int)|Removes the element at the specified position in
56 |java.util.Vector.remove(Object)|Removes the first occurrence of the specified
57 |java.util.Vector.removeAll(Collection<?>)|Removes from this Vector all of its
58 |java.util.Vector.removeAllElements()|Removes all components from this vector a
59 |java.util.Vector.removeElement(Object)|Removes the first (lowest-indexed) occu
60 |java.util.Vector.removeElementAt(int)|Deletes the component at the specified i
61 |java.util.Vector.removeRange(int,int)|Removes from this List all of the elemen
62 |java.util.Vector.retainAll(Collection<?>)|Retains only the elements in this Ve
63 |java.util.Vector.set(int,E)|Replaces the element at the specified position in
64 |java.util.Vector.setElementAt(E,int)|Sets the component at the specifiedindexo
65 |java.util.Vector.setSize(int)|Sets the size of this vector.
66 |java.util.Vector.size()|Returns the number of components in this vector.
67 |java.util.Vector.subList(int,int)|Returns a view of the portion of this List b
68 |java.util.Vector.toArray()|Returns an array containing all of the elements in
69 |java.util.Vector.toArray(T[])|Returns an array containing all of the elements
70 |java.util.Vector.toString()|Returns a string representation of this Vector, co
71 |java.util.Vector.trimToSize()|Trims the capacity of this vector to be the vect
73 *java.util.Vector_Description*
75 TheVectorclass implements a growable array of objects. Like an array, it
76 contains components that can be accessed using an integer index. However, the
77 size of aVectorcan grow or shrink as needed to accommodate adding and removing
78 items after theVectorhas been created.
80 Each vector tries to optimize storage management by maintaining acapacityand
81 acapacityIncrement. Thecapacityis always at least as large as the vector size;
82 it is usually larger because as components are added to the vector, the
83 vector's storage increases in chunks the size ofcapacityIncrement. An
84 application can increase the capacity of a vector before inserting a large
85 number of components; this reduces the amount of incremental reallocation.
87 The Iterators returned by Vector's iterator and listIterator methods are
88 fail-fast: if the Vector is structurally modified at any time after the
89 Iterator is created, in any way except through the Iterator's own remove or add
90 methods, the Iterator will throw a ConcurrentModificationException. Thus, in
91 the face of concurrent modification, the Iterator fails quickly and cleanly,
92 rather than risking arbitrary, non-deterministic behavior at an undetermined
93 time in the future. The Enumerations returned by Vector's elements method are
96 Note that the fail-fast behavior of an iterator cannot be guaranteed as it is,
97 generally speaking, impossible to make any hard guarantees in the presence of
98 unsynchronized concurrent modification. Fail-fast iterators
99 throwConcurrentModificationExceptionon a best-effort basis. Therefore, it would
100 be wrong to write a program that depended on this exception for its
101 correctness: the fail-fast behavior of iterators should be used only to detect
104 As of the Java 2 platform v1.2, this class was retrofitted to implement the
105 (|java.util.List|) interface, making it a member of the <a
106 href="/../technotes/guides/collections/index.html"> Java Collections Framework.
107 Unlike the new collection implementations,Vectoris synchronized.
111 *int_java.util.Vector.capacityIncrement*
113 The amount by which the capacity of the vector is automatically incremented
114 when its size becomes greater than its capacity. If the capacity increment is
115 less than or equal to zero, the capacity of the vector is doubled each time it
119 *int_java.util.Vector.elementCount*
121 The number of valid components in thisVectorobject.
122 ComponentselementData[0]throughelementData[elementCount-1]are the actual items.
125 *java.lang.Object[]_java.util.Vector.elementData*
127 The array buffer into which the components of the vector are stored. The
128 capacity of the vector is the length of this array buffer, and is at least
129 large enough to contain all the vector's elements.
131 Any array elements following the last element in the Vector are null.
139 Constructs an empty vector so that its internal data array has size10and its
140 standard capacity increment is zero.
143 *java.util.Vector(Collection<?extendsE>)*
145 public Vector(java.util.Collection<? extends E> c)
147 Constructs a vector containing the elements of the specified collection, in the
148 order they are returned by the collection's iterator.
150 c - the collection whose elements are to be placed into this vector
152 *java.util.Vector(int)*
154 public Vector(int initialCapacity)
156 Constructs an empty vector with the specified initial capacity and with its
157 capacity increment equal to zero.
159 initialCapacity - the initial capacity of the vector
161 *java.util.Vector(int,int)*
165 int capacityIncrement)
167 Constructs an empty vector with the specified initial capacity and capacity
170 initialCapacity - the initial capacity of the vector
171 capacityIncrement - the amount by which the capacity is increased when the vector overflows
173 *java.util.Vector.add(E)*
175 public synchronized boolean add(E e)
177 Appends the specified element to the end of this Vector.
180 e - element to be appended to this Vector
182 Returns: {@code true} (as specified by {@link Collection#add})
184 *java.util.Vector.add(int,E)*
190 Inserts the specified element at the specified position in this Vector. Shifts
191 the element currently at that position (if any) and any subsequent elements to
192 the right (adds one to their indices).
195 index - index at which the specified element is to be inserted
196 element - element to be inserted
198 *java.util.Vector.addAll(Collection<?extendsE>)*
200 public synchronized boolean addAll(java.util.Collection<? extends E> c)
202 Appends all of the elements in the specified Collection to the end of this
203 Vector, in the order that they are returned by the specified Collection's
204 Iterator. The behavior of this operation is undefined if the specified
205 Collection is modified while the operation is in progress. (This implies that
206 the behavior of this call is undefined if the specified Collection is this
207 Vector, and this Vector is nonempty.)
210 c - elements to be inserted into this Vector
212 Returns: {@code true} if this Vector changed as a result of the call
214 *java.util.Vector.addAll(int,Collection<?extendsE>)*
216 public synchronized boolean addAll(
218 java.util.Collection<? extends E> c)
220 Inserts all of the elements in the specified Collection into this Vector at the
221 specified position. Shifts the element currently at that position (if any) and
222 any subsequent elements to the right (increases their indices). The new
223 elements will appear in the Vector in the order that they are returned by the
224 specified Collection's iterator.
227 index - index at which to insert the first element from the specified collection
228 c - elements to be inserted into this Vector
230 Returns: {@code true} if this Vector changed as a result of the call
232 *java.util.Vector.addElement(E)*
234 public synchronized void addElement(E obj)
236 Adds the specified component to the end of this vector, increasing its size by
237 one. The capacity of this vector is increased if its size becomes greater than
240 This method is identical in functionality to the add(E)(|java.util.Vector|)
241 method (which is part of the (|java.util.List|) interface).
244 obj - the component to be added
246 *java.util.Vector.capacity()*
248 public synchronized int capacity()
250 Returns the current capacity of this vector.
254 Returns: the current capacity (the length of its internal data array, kept in the field
255 {@code elementData} of this vector)
257 *java.util.Vector.clear()*
261 Removes all of the elements from this Vector. The Vector will be empty after
262 this call returns (unless it throws an exception).
266 *java.util.Vector.clone()*
268 public synchronized |java.lang.Object| clone()
270 Returns a clone of this vector. The copy will contain a reference to a clone of
271 the internal data array, not a reference to the original internal data array of
276 Returns: a clone of this vector
278 *java.util.Vector.contains(Object)*
280 public boolean contains(java.lang.Object o)
282 Returnstrueif this vector contains the specified element. More formally,
283 returnstrueif and only if this vector contains at least one elementesuch that
284 (o==null?e==null:o.equals(e)).
287 o - element whose presence in this vector is to be tested
289 Returns: {@code true} if this vector contains the specified element
291 *java.util.Vector.containsAll(Collection<?>)*
293 public synchronized boolean containsAll(java.util.Collection<?> c)
295 Returns true if this Vector contains all of the elements in the specified
299 c - a collection whose elements will be tested for containment in this Vector
301 Returns: true if this Vector contains all of the elements in the specified collection
303 *java.util.Vector.copyInto(Object[])*
305 public synchronized void copyInto(java.lang.Object[] anArray)
307 Copies the components of this vector into the specified array. The item at
308 indexkin this vector is copied into componentkofanArray.
311 anArray - the array into which the components get copied
313 *java.util.Vector.elementAt(int)*
315 public synchronized |E| elementAt(int index)
317 Returns the component at the specified index.
319 This method is identical in functionality to the (|java.util.Vector|) method
320 (which is part of the (|java.util.List|) interface).
323 index - an index into this vector
325 Returns: the component at the specified index
327 *java.util.Vector.elements()*
329 public |java.util.Enumeration|<E> elements()
331 Returns an enumeration of the components of this vector. The
332 returnedEnumerationobject will generate all items in this vector. The first
333 item generated is the item at index0, then the item at index1, and so on.
337 Returns: an enumeration of the components of this vector
339 *java.util.Vector.ensureCapacity(int)*
341 public synchronized void ensureCapacity(int minCapacity)
343 Increases the capacity of this vector, if necessary, to ensure that it can hold
344 at least the number of components specified by the minimum capacity argument.
346 If the current capacity of this vector is less thanminCapacity, then its
347 capacity is increased by replacing its internal data array, kept in the
348 fieldelementData, with a larger one. The size of the new data array will be the
349 old size pluscapacityIncrement, unless the value ofcapacityIncrementis less
350 than or equal to zero, in which case the new capacity will be twice the old
351 capacity; but if this new size is still smaller thanminCapacity, then the new
352 capacity will beminCapacity.
355 minCapacity - the desired minimum capacity
357 *java.util.Vector.equals(Object)*
359 public synchronized boolean equals(java.lang.Object o)
361 Compares the specified Object with this Vector for equality. Returns true if
362 and only if the specified Object is also a List, both Lists have the same size,
363 and all corresponding pairs of elements in the two Lists are equal. (Two
364 elementse1ande2are equal if(e1==null ? e2==null : e1.equals(e2)).) In other
365 words, two Lists are defined to be equal if they contain the same elements in
369 o - the Object to be compared for equality with this Vector
371 Returns: true if the specified Object is equal to this Vector
373 *java.util.Vector.firstElement()*
375 public synchronized |E| firstElement()
377 Returns the first component (the item at index0) of this vector.
381 Returns: the first component of this vector
383 *java.util.Vector.get(int)*
385 public synchronized |E| get(int index)
387 Returns the element at the specified position in this Vector.
390 index - index of the element to return
392 Returns: object at the specified index
394 *java.util.Vector.hashCode()*
396 public synchronized int hashCode()
398 Returns the hash code value for this Vector.
402 *java.util.Vector.indexOf(Object)*
404 public int indexOf(java.lang.Object o)
406 Returns the index of the first occurrence of the specified element in this
407 vector, or -1 if this vector does not contain the element. More formally,
408 returns the lowest indexisuch that (o==null?get(i)==null:o.equals(get(i))), or
409 -1 if there is no such index.
412 o - element to search for
414 Returns: the index of the first occurrence of the specified element in this vector, or
415 -1 if this vector does not contain the element
417 *java.util.Vector.indexOf(Object,int)*
419 public synchronized int indexOf(
423 Returns the index of the first occurrence of the specified element in this
424 vector, searching forwards fromindex, or returns -1 if the element is not
425 found. More formally, returns the lowest indexisuch that (i>=index and and
426 (o==null?get(i)==null:o.equals(get(i)))), or -1 if there is no such index.
429 o - element to search for
430 index - index to start searching from
432 Returns: the index of the first occurrence of the element in this vector at position
433 {@code index} or later in the vector; {@code -1} if the element is
436 *java.util.Vector.insertElementAt(E,int)*
438 public synchronized void insertElementAt(
442 Inserts the specified object as a component in this vector at the
443 specifiedindex. Each component in this vector with an index greater or equal to
444 the specifiedindexis shifted upward to have an index one greater than the value
447 The index must be a value greater than or equal to0and less than or equal to
448 the current size of the vector. (If the index is equal to the current size of
449 the vector, the new element is appended to the Vector.)
451 This method is identical in functionality to the add(int,
452 E)(|java.util.Vector|) method (which is part of the (|java.util.List|)
453 interface). Note that theaddmethod reverses the order of the parameters, to
454 more closely match array usage.
457 obj - the component to insert
458 index - where to insert the new component
460 *java.util.Vector.isEmpty()*
462 public synchronized boolean isEmpty()
464 Tests if this vector has no components.
468 Returns: {@code true} if and only if this vector has no components, that is, its size is
469 zero; {@code false} otherwise.
471 *java.util.Vector.lastElement()*
473 public synchronized |E| lastElement()
475 Returns the last component of the vector.
479 Returns: the last component of the vector, i.e., the component at index size()-1.
481 *java.util.Vector.lastIndexOf(Object)*
483 public synchronized int lastIndexOf(java.lang.Object o)
485 Returns the index of the last occurrence of the specified element in this
486 vector, or -1 if this vector does not contain the element. More formally,
487 returns the highest indexisuch that (o==null?get(i)==null:o.equals(get(i))), or
488 -1 if there is no such index.
491 o - element to search for
493 Returns: the index of the last occurrence of the specified element in this vector, or -1
494 if this vector does not contain the element
496 *java.util.Vector.lastIndexOf(Object,int)*
498 public synchronized int lastIndexOf(
502 Returns the index of the last occurrence of the specified element in this
503 vector, searching backwards fromindex, or returns -1 if the element is not
504 found. More formally, returns the highest indexisuch that (i<=index and and
505 (o==null?get(i)==null:o.equals(get(i)))), or -1 if there is no such index.
508 o - element to search for
509 index - index to start searching backwards from
511 Returns: the index of the last occurrence of the element at position less than or equal
512 to {@code index} in this vector; -1 if the element is not found.
514 *java.util.Vector.remove(int)*
516 public synchronized |E| remove(int index)
518 Removes the element at the specified position in this Vector. Shifts any
519 subsequent elements to the left (subtracts one from their indices). Returns the
520 element that was removed from the Vector.
523 index - the index of the element to be removed
525 Returns: element that was removed
527 *java.util.Vector.remove(Object)*
529 public boolean remove(java.lang.Object o)
531 Removes the first occurrence of the specified element in this Vector If the
532 Vector does not contain the element, it is unchanged. More formally, removes
533 the element with the lowest index i such that(o==null ? get(i)==null :
534 o.equals(get(i)))(if such an element exists).
537 o - element to be removed from this Vector, if present
539 Returns: true if the Vector contained the specified element
541 *java.util.Vector.removeAll(Collection<?>)*
543 public synchronized boolean removeAll(java.util.Collection<?> c)
545 Removes from this Vector all of its elements that are contained in the
546 specified Collection.
549 c - a collection of elements to be removed from the Vector
551 Returns: true if this Vector changed as a result of the call
553 *java.util.Vector.removeAllElements()*
555 public synchronized void removeAllElements()
557 Removes all components from this vector and sets its size to zero.
559 This method is identical in functionality to the (|java.util.Vector|) method
560 (which is part of the (|java.util.List|) interface).
564 *java.util.Vector.removeElement(Object)*
566 public synchronized boolean removeElement(java.lang.Object obj)
568 Removes the first (lowest-indexed) occurrence of the argument from this vector.
569 If the object is found in this vector, each component in the vector with an
570 index greater or equal to the object's index is shifted downward to have an
571 index one smaller than the value it had previously.
573 This method is identical in functionality to the (|java.util.Vector|) method
574 (which is part of the (|java.util.List|) interface).
577 obj - the component to be removed
579 Returns: {@code true} if the argument was a component of this vector; {@code false}
582 *java.util.Vector.removeElementAt(int)*
584 public synchronized void removeElementAt(int index)
586 Deletes the component at the specified index. Each component in this vector
587 with an index greater or equal to the specifiedindexis shifted downward to have
588 an index one smaller than the value it had previously. The size of this vector
591 The index must be a value greater than or equal to0and less than the current
594 This method is identical in functionality to the (|java.util.Vector|) method
595 (which is part of the (|java.util.List|) interface). Note that theremovemethod
596 returns the old value that was stored at the specified position.
599 index - the index of the object to remove
601 *java.util.Vector.removeRange(int,int)*
603 protected synchronized void removeRange(
607 Removes from this List all of the elements whose index is between fromIndex,
608 inclusive and toIndex, exclusive. Shifts any succeeding elements to the left
609 (reduces their index). This call shortens the ArrayList by (toIndex -
610 fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)
613 fromIndex - index of first element to be removed
614 toIndex - index after last element to be removed
616 *java.util.Vector.retainAll(Collection<?>)*
618 public synchronized boolean retainAll(java.util.Collection<?> c)
620 Retains only the elements in this Vector that are contained in the specified
621 Collection. In other words, removes from this Vector all of its elements that
622 are not contained in the specified Collection.
625 c - a collection of elements to be retained in this Vector (all other elements are
628 Returns: true if this Vector changed as a result of the call
630 *java.util.Vector.set(int,E)*
632 public synchronized |E| set(
636 Replaces the element at the specified position in this Vector with the
640 index - index of the element to replace
641 element - element to be stored at the specified position
643 Returns: the element previously at the specified position
645 *java.util.Vector.setElementAt(E,int)*
647 public synchronized void setElementAt(
651 Sets the component at the specifiedindexof this vector to be the specified
652 object. The previous component at that position is discarded.
654 The index must be a value greater than or equal to0and less than the current
657 This method is identical in functionality to the set(int,
658 E)(|java.util.Vector|) method (which is part of the (|java.util.List|)
659 interface). Note that thesetmethod reverses the order of the parameters, to
660 more closely match array usage. Note also that thesetmethod returns the old
661 value that was stored at the specified position.
664 obj - what the component is to be set to
665 index - the specified index
667 *java.util.Vector.setSize(int)*
669 public synchronized void setSize(int newSize)
671 Sets the size of this vector. If the new size is greater than the current size,
672 newnullitems are added to the end of the vector. If the new size is less than
673 the current size, all components at indexnewSizeand greater are discarded.
676 newSize - the new size of this vector
678 *java.util.Vector.size()*
680 public synchronized int size()
682 Returns the number of components in this vector.
686 Returns: the number of components in this vector
688 *java.util.Vector.subList(int,int)*
690 public synchronized |java.util.List|<E> subList(
694 Returns a view of the portion of this List between fromIndex, inclusive, and
695 toIndex, exclusive. (If fromIndex and toIndex are equal, the returned List is
696 empty.) The returned List is backed by this List, so changes in the returned
697 List are reflected in this List, and vice-versa. The returned List supports all
698 of the optional List operations supported by this List.
700 This method eliminates the need for explicit range operations (of the sort that
701 commonly exist for arrays). Any operation that expects a List can be used as a
702 range operation by operating on a subList view instead of a whole List. For
703 example, the following idiom removes a range of elements from a List:
705 list.subList(from, to).clear();
707 Similar idioms may be constructed for indexOf and lastIndexOf, and all of the
708 algorithms in the Collections class can be applied to a subList.
710 The semantics of the List returned by this method become undefined if the
711 backing list (i.e., this List) is structurally modified in any way other than
712 via the returned List. (Structural modifications are those that change the size
713 of the List, or otherwise perturb it in such a fashion that iterations in
714 progress may yield incorrect results.)
717 fromIndex - low endpoint (inclusive) of the subList
718 toIndex - high endpoint (exclusive) of the subList
720 Returns: a view of the specified range within this List
722 *java.util.Vector.toArray()*
724 public synchronized |java.lang.Object|[] toArray()
726 Returns an array containing all of the elements in this Vector in the correct
731 *java.util.Vector.toArray(T[])*
733 public synchronized |T|[] toArray(T[] a)
735 Returns an array containing all of the elements in this Vector in the correct
736 order; the runtime type of the returned array is that of the specified array.
737 If the Vector fits in the specified array, it is returned therein. Otherwise, a
738 new array is allocated with the runtime type of the specified array and the
741 If the Vector fits in the specified array with room to spare (i.e., the array
742 has more elements than the Vector), the element in the array immediately
743 following the end of the Vector is set to null. (This is useful in determining
744 the length of the Vector only if the caller knows that the Vector does not
745 contain any null elements.)
748 a - the array into which the elements of the Vector are to be stored, if it is big
749 enough; otherwise, a new array of the same runtime type is allocated for
752 Returns: an array containing the elements of the Vector
754 *java.util.Vector.toString()*
756 public synchronized |java.lang.String| toString()
758 Returns a string representation of this Vector, containing the String
759 representation of each element.
763 *java.util.Vector.trimToSize()*
765 public synchronized void trimToSize()
767 Trims the capacity of this vector to be the vector's current size. If the
768 capacity of this vector is larger than its current size, then the capacity is
769 changed to equal the size by replacing its internal data array, kept in the
770 fieldelementData, with a smaller one. An application can use this operation to
771 minimize the storage of a vector.