54 * {@link ListIterator#add(Object) add} methods, the iterator will throw a |
54 * {@link ListIterator#add(Object) add} methods, the iterator will throw a |
55 * {@link ConcurrentModificationException}. Thus, in the face of |
55 * {@link ConcurrentModificationException}. Thus, in the face of |
56 * concurrent modification, the iterator fails quickly and cleanly, rather |
56 * concurrent modification, the iterator fails quickly and cleanly, rather |
57 * than risking arbitrary, non-deterministic behavior at an undetermined |
57 * than risking arbitrary, non-deterministic behavior at an undetermined |
58 * time in the future. The {@link Enumeration Enumerations} returned by |
58 * time in the future. The {@link Enumeration Enumerations} returned by |
59 * the {@link #elements() elements} method are <em>not</em> fail-fast. |
59 * the {@link #elements() elements} method are <em>not</em> fail-fast; if the |
|
60 * Vector is structurally modified at any time after the enumeration is |
|
61 * created then the results of enumerating are undefined. |
60 * |
62 * |
61 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
63 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed |
62 * as it is, generally speaking, impossible to make any hard guarantees in the |
64 * as it is, generally speaking, impossible to make any hard guarantees in the |
63 * presence of unsynchronized concurrent modification. Fail-fast iterators |
65 * presence of unsynchronized concurrent modification. Fail-fast iterators |
64 * throw {@code ConcurrentModificationException} on a best-effort basis. |
66 * throw {@code ConcurrentModificationException} on a best-effort basis. |
72 * Java Collections Framework</a>. Unlike the new collection |
74 * Java Collections Framework</a>. Unlike the new collection |
73 * implementations, {@code Vector} is synchronized. If a thread-safe |
75 * implementations, {@code Vector} is synchronized. If a thread-safe |
74 * implementation is not needed, it is recommended to use {@link |
76 * implementation is not needed, it is recommended to use {@link |
75 * ArrayList} in place of {@code Vector}. |
77 * ArrayList} in place of {@code Vector}. |
76 * |
78 * |
|
79 * @param <E> Type of component elements |
|
80 * |
77 * @author Lee Boynton |
81 * @author Lee Boynton |
78 * @author Jonathan Payne |
82 * @author Jonathan Payne |
79 * @see Collection |
83 * @see Collection |
80 * @see LinkedList |
84 * @see LinkedList |
81 * @since JDK1.0 |
85 * @since JDK1.0 |
328 |
332 |
329 /** |
333 /** |
330 * Returns an enumeration of the components of this vector. The |
334 * Returns an enumeration of the components of this vector. The |
331 * returned {@code Enumeration} object will generate all items in |
335 * returned {@code Enumeration} object will generate all items in |
332 * this vector. The first item generated is the item at index {@code 0}, |
336 * this vector. The first item generated is the item at index {@code 0}, |
333 * then the item at index {@code 1}, and so on. |
337 * then the item at index {@code 1}, and so on. If the vector is |
|
338 * structurally modified while enumerating over the elements then the |
|
339 * results of enumerating are undefined. |
334 * |
340 * |
335 * @return an enumeration of the components of this vector |
341 * @return an enumeration of the components of this vector |
336 * @see Iterator |
342 * @see Iterator |
337 */ |
343 */ |
338 public Enumeration<E> elements() { |
344 public Enumeration<E> elements() { |
551 * @param index the index of the object to remove |
557 * @param index the index of the object to remove |
552 * @throws ArrayIndexOutOfBoundsException if the index is out of range |
558 * @throws ArrayIndexOutOfBoundsException if the index is out of range |
553 * ({@code index < 0 || index >= size()}) |
559 * ({@code index < 0 || index >= size()}) |
554 */ |
560 */ |
555 public synchronized void removeElementAt(int index) { |
561 public synchronized void removeElementAt(int index) { |
556 modCount++; |
|
557 if (index >= elementCount) { |
562 if (index >= elementCount) { |
558 throw new ArrayIndexOutOfBoundsException(index + " >= " + |
563 throw new ArrayIndexOutOfBoundsException(index + " >= " + |
559 elementCount); |
564 elementCount); |
560 } |
565 } |
561 else if (index < 0) { |
566 else if (index < 0) { |
563 } |
568 } |
564 int j = elementCount - index - 1; |
569 int j = elementCount - index - 1; |
565 if (j > 0) { |
570 if (j > 0) { |
566 System.arraycopy(elementData, index + 1, elementData, index, j); |
571 System.arraycopy(elementData, index + 1, elementData, index, j); |
567 } |
572 } |
|
573 modCount++; |
568 elementCount--; |
574 elementCount--; |
569 elementData[elementCount] = null; /* to let gc do its work */ |
575 elementData[elementCount] = null; /* to let gc do its work */ |
570 } |
576 } |
571 |
577 |
572 /** |
578 /** |
591 * @param index where to insert the new component |
597 * @param index where to insert the new component |
592 * @throws ArrayIndexOutOfBoundsException if the index is out of range |
598 * @throws ArrayIndexOutOfBoundsException if the index is out of range |
593 * ({@code index < 0 || index > size()}) |
599 * ({@code index < 0 || index > size()}) |
594 */ |
600 */ |
595 public synchronized void insertElementAt(E obj, int index) { |
601 public synchronized void insertElementAt(E obj, int index) { |
596 modCount++; |
|
597 if (index > elementCount) { |
602 if (index > elementCount) { |
598 throw new ArrayIndexOutOfBoundsException(index |
603 throw new ArrayIndexOutOfBoundsException(index |
599 + " > " + elementCount); |
604 + " > " + elementCount); |
600 } |
605 } |
601 ensureCapacityHelper(elementCount + 1); |
606 ensureCapacityHelper(elementCount + 1); |
602 System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); |
607 System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); |
603 elementData[index] = obj; |
608 elementData[index] = obj; |
|
609 modCount++; |
604 elementCount++; |
610 elementCount++; |
605 } |
611 } |
606 |
612 |
607 /** |
613 /** |
608 * Adds the specified component to the end of this vector, |
614 * Adds the specified component to the end of this vector, |
614 * method (which is part of the {@link List} interface). |
620 * method (which is part of the {@link List} interface). |
615 * |
621 * |
616 * @param obj the component to be added |
622 * @param obj the component to be added |
617 */ |
623 */ |
618 public synchronized void addElement(E obj) { |
624 public synchronized void addElement(E obj) { |
|
625 ensureCapacityHelper(elementCount + 1); |
619 modCount++; |
626 modCount++; |
620 ensureCapacityHelper(elementCount + 1); |
|
621 elementData[elementCount++] = obj; |
627 elementData[elementCount++] = obj; |
622 } |
628 } |
623 |
629 |
624 /** |
630 /** |
625 * Removes the first (lowest-indexed) occurrence of the argument |
631 * Removes the first (lowest-indexed) occurrence of the argument |
651 * |
657 * |
652 * <p>This method is identical in functionality to the {@link #clear} |
658 * <p>This method is identical in functionality to the {@link #clear} |
653 * method (which is part of the {@link List} interface). |
659 * method (which is part of the {@link List} interface). |
654 */ |
660 */ |
655 public synchronized void removeAllElements() { |
661 public synchronized void removeAllElements() { |
656 modCount++; |
|
657 // Let gc do its work |
662 // Let gc do its work |
658 for (int i = 0; i < elementCount; i++) |
663 for (int i = 0; i < elementCount; i++) |
659 elementData[i] = null; |
664 elementData[i] = null; |
660 |
665 |
|
666 modCount++; |
661 elementCount = 0; |
667 elementCount = 0; |
662 } |
668 } |
663 |
669 |
664 /** |
670 /** |
665 * Returns a clone of this vector. The copy will contain a |
671 * Returns a clone of this vector. The copy will contain a |
703 * the element in the array immediately following the end of the |
709 * the element in the array immediately following the end of the |
704 * Vector is set to null. (This is useful in determining the length |
710 * Vector is set to null. (This is useful in determining the length |
705 * of the Vector <em>only</em> if the caller knows that the Vector |
711 * of the Vector <em>only</em> if the caller knows that the Vector |
706 * does not contain any null elements.) |
712 * does not contain any null elements.) |
707 * |
713 * |
|
714 * @param <T> type of array elements. The same type as {@code <E>} or a |
|
715 * supertype of {@code <E>}. |
708 * @param a the array into which the elements of the Vector are to |
716 * @param a the array into which the elements of the Vector are to |
709 * be stored, if it is big enough; otherwise, a new array of the |
717 * be stored, if it is big enough; otherwise, a new array of the |
710 * same runtime type is allocated for this purpose. |
718 * same runtime type is allocated for this purpose. |
711 * @return an array containing the elements of the Vector |
719 * @return an array containing the elements of the Vector |
712 * @throws ArrayStoreException if the runtime type of a is not a supertype |
720 * @throws ArrayStoreException if the runtime type of a, {@code <T>}, is not |
713 * of the runtime type of every element in this Vector |
721 * a supertype of the runtime type, {@code <E>}, of every element in this |
|
722 * Vector |
714 * @throws NullPointerException if the given array is null |
723 * @throws NullPointerException if the given array is null |
715 * @since 1.2 |
724 * @since 1.2 |
716 */ |
725 */ |
717 @SuppressWarnings("unchecked") |
726 @SuppressWarnings("unchecked") |
718 public synchronized <T> T[] toArray(T[] a) { |
727 public synchronized <T> T[] toArray(T[] a) { |
776 * @param e element to be appended to this Vector |
785 * @param e element to be appended to this Vector |
777 * @return {@code true} (as specified by {@link Collection#add}) |
786 * @return {@code true} (as specified by {@link Collection#add}) |
778 * @since 1.2 |
787 * @since 1.2 |
779 */ |
788 */ |
780 public synchronized boolean add(E e) { |
789 public synchronized boolean add(E e) { |
|
790 ensureCapacityHelper(elementCount + 1); |
781 modCount++; |
791 modCount++; |
782 ensureCapacityHelper(elementCount + 1); |
|
783 elementData[elementCount++] = e; |
792 elementData[elementCount++] = e; |
784 return true; |
793 return true; |
785 } |
794 } |
786 |
795 |
787 /** |
796 /** |
877 * @param c elements to be inserted into this Vector |
886 * @param c elements to be inserted into this Vector |
878 * @return {@code true} if this Vector changed as a result of the call |
887 * @return {@code true} if this Vector changed as a result of the call |
879 * @throws NullPointerException if the specified collection is null |
888 * @throws NullPointerException if the specified collection is null |
880 * @since 1.2 |
889 * @since 1.2 |
881 */ |
890 */ |
882 public synchronized boolean addAll(Collection<? extends E> c) { |
891 public boolean addAll(Collection<? extends E> c) { |
883 modCount++; |
|
884 Object[] a = c.toArray(); |
892 Object[] a = c.toArray(); |
885 int numNew = a.length; |
893 int numNew = a.length; |
886 ensureCapacityHelper(elementCount + numNew); |
894 if (numNew > 0) { |
887 System.arraycopy(a, 0, elementData, elementCount, numNew); |
895 synchronized (this) { |
888 elementCount += numNew; |
896 ensureCapacityHelper(elementCount + numNew); |
889 return numNew != 0; |
897 System.arraycopy(a, 0, elementData, elementCount, numNew); |
|
898 modCount++; |
|
899 elementCount += numNew; |
|
900 } |
|
901 } |
|
902 return numNew > 0; |
890 } |
903 } |
891 |
904 |
892 /** |
905 /** |
893 * Removes from this Vector all of its elements that are contained in the |
906 * Removes from this Vector all of its elements that are contained in the |
894 * specified Collection. |
907 * specified Collection. |
949 * ({@code index < 0 || index > size()}) |
962 * ({@code index < 0 || index > size()}) |
950 * @throws NullPointerException if the specified collection is null |
963 * @throws NullPointerException if the specified collection is null |
951 * @since 1.2 |
964 * @since 1.2 |
952 */ |
965 */ |
953 public synchronized boolean addAll(int index, Collection<? extends E> c) { |
966 public synchronized boolean addAll(int index, Collection<? extends E> c) { |
954 modCount++; |
|
955 if (index < 0 || index > elementCount) |
967 if (index < 0 || index > elementCount) |
956 throw new ArrayIndexOutOfBoundsException(index); |
968 throw new ArrayIndexOutOfBoundsException(index); |
957 |
969 |
958 Object[] a = c.toArray(); |
970 Object[] a = c.toArray(); |
959 int numNew = a.length; |
971 int numNew = a.length; |
960 ensureCapacityHelper(elementCount + numNew); |
972 |
961 |
973 if (numNew > 0) { |
962 int numMoved = elementCount - index; |
974 ensureCapacityHelper(elementCount + numNew); |
963 if (numMoved > 0) |
975 |
964 System.arraycopy(elementData, index, elementData, index + numNew, |
976 int numMoved = elementCount - index; |
965 numMoved); |
977 if (numMoved > 0) |
966 |
978 System.arraycopy(elementData, index, elementData, |
967 System.arraycopy(a, 0, elementData, index, numNew); |
979 index + numNew, numMoved); |
968 elementCount += numNew; |
980 |
969 return numNew != 0; |
981 System.arraycopy(a, 0, elementData, index, numNew); |
|
982 elementCount += numNew; |
|
983 modCount++; |
|
984 } |
|
985 return numNew > 0; |
970 } |
986 } |
971 |
987 |
972 /** |
988 /** |
973 * Compares the specified Object with this Vector for equality. Returns |
989 * Compares the specified Object with this Vector for equality. Returns |
974 * true if and only if the specified Object is also a List, both Lists |
990 * true if and only if the specified Object is also a List, both Lists |
1045 * Shifts any succeeding elements to the left (reduces their index). |
1061 * Shifts any succeeding elements to the left (reduces their index). |
1046 * This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1062 * This call shortens the list by {@code (toIndex - fromIndex)} elements. |
1047 * (If {@code toIndex==fromIndex}, this operation has no effect.) |
1063 * (If {@code toIndex==fromIndex}, this operation has no effect.) |
1048 */ |
1064 */ |
1049 protected synchronized void removeRange(int fromIndex, int toIndex) { |
1065 protected synchronized void removeRange(int fromIndex, int toIndex) { |
1050 modCount++; |
|
1051 int numMoved = elementCount - toIndex; |
1066 int numMoved = elementCount - toIndex; |
1052 System.arraycopy(elementData, toIndex, elementData, fromIndex, |
1067 System.arraycopy(elementData, toIndex, elementData, fromIndex, |
1053 numMoved); |
1068 numMoved); |
1054 |
1069 |
1055 // Let gc do its work |
1070 // Let gc do its work |
|
1071 modCount++; |
1056 int newElementCount = elementCount - (toIndex-fromIndex); |
1072 int newElementCount = elementCount - (toIndex-fromIndex); |
1057 while (elementCount != newElementCount) |
1073 while (elementCount != newElementCount) |
1058 elementData[--elementCount] = null; |
1074 elementData[--elementCount] = null; |
1059 } |
1075 } |
1060 |
1076 |
1418 } |
1434 } |
1419 throw new ConcurrentModificationException(); |
1435 throw new ConcurrentModificationException(); |
1420 } |
1436 } |
1421 |
1437 |
1422 public long estimateSize() { |
1438 public long estimateSize() { |
1423 return (long) (getFence() - index); |
1439 return getFence() - index; |
1424 } |
1440 } |
1425 |
1441 |
1426 public int characteristics() { |
1442 public int characteristics() { |
1427 return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1443 return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED; |
1428 } |
1444 } |