536 /**
537 * Deletes the component at the specified index. Each component in
538 * this vector with an index greater or equal to the specified
539 * {@code index} is shifted downward to have an index one
540 * smaller than the value it had previously. The size of this vector
541 * is decreased by {@code 1}.
542 *
543 * <p>The index must be a value greater than or equal to {@code 0}
544 * and less than the current size of the vector.
545 *
546 * <p>This method is identical in functionality to the {@link #remove(int)}
547 * method (which is part of the {@link List} interface). Note that the
548 * {@code remove} method returns the old value that was stored at the
549 * specified position.
550 *
551 * @param index the index of the object to remove
552 * @throws ArrayIndexOutOfBoundsException if the index is out of range
553 * ({@code index < 0 || index >= size()})
554 */
555 public synchronized void removeElementAt(int index) {
556 modCount++;
557 if (index >= elementCount) {
558 throw new ArrayIndexOutOfBoundsException(index + " >= " +
559 elementCount);
560 }
561 else if (index < 0) {
562 throw new ArrayIndexOutOfBoundsException(index);
563 }
564 int j = elementCount - index - 1;
565 if (j > 0) {
566 System.arraycopy(elementData, index + 1, elementData, index, j);
567 }
568 elementCount--;
569 elementData[elementCount] = null; /* to let gc do its work */
570 }
571
572 /**
573 * Inserts the specified object as a component in this vector at the
574 * specified {@code index}. Each component in this vector with
575 * an index greater or equal to the specified {@code index} is
576 * shifted upward to have an index one greater than the value it had
577 * previously.
578 *
579 * <p>The index must be a value greater than or equal to {@code 0}
580 * and less than or equal to the current size of the vector. (If the
581 * index is equal to the current size of the vector, the new element
582 * is appended to the Vector.)
583 *
584 * <p>This method is identical in functionality to the
585 * {@link #add(int, Object) add(int, E)}
586 * method (which is part of the {@link List} interface). Note that the
587 * {@code add} method reverses the order of the parameters, to more closely
588 * match array usage.
589 *
590 * @param obj the component to insert
591 * @param index where to insert the new component
592 * @throws ArrayIndexOutOfBoundsException if the index is out of range
593 * ({@code index < 0 || index > size()})
594 */
595 public synchronized void insertElementAt(E obj, int index) {
596 modCount++;
597 if (index > elementCount) {
598 throw new ArrayIndexOutOfBoundsException(index
599 + " > " + elementCount);
600 }
601 ensureCapacityHelper(elementCount + 1);
602 System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
603 elementData[index] = obj;
604 elementCount++;
605 }
606
607 /**
608 * Adds the specified component to the end of this vector,
609 * increasing its size by one. The capacity of this vector is
610 * increased if its size becomes greater than its capacity.
611 *
612 * <p>This method is identical in functionality to the
613 * {@link #add(Object) add(E)}
614 * method (which is part of the {@link List} interface).
615 *
616 * @param obj the component to be added
617 */
618 public synchronized void addElement(E obj) {
619 modCount++;
620 ensureCapacityHelper(elementCount + 1);
621 elementData[elementCount++] = obj;
622 }
623
624 /**
625 * Removes the first (lowest-indexed) occurrence of the argument
626 * from this vector. If the object is found in this vector, each
627 * component in the vector with an index greater or equal to the
628 * object's index is shifted downward to have an index one smaller
629 * than the value it had previously.
630 *
631 * <p>This method is identical in functionality to the
632 * {@link #remove(Object)} method (which is part of the
633 * {@link List} interface).
634 *
635 * @param obj the component to be removed
636 * @return {@code true} if the argument was a component of this
637 * vector; {@code false} otherwise.
638 */
639 public synchronized boolean removeElement(Object obj) {
640 modCount++;
641 int i = indexOf(obj);
642 if (i >= 0) {
643 removeElementAt(i);
644 return true;
645 }
646 return false;
647 }
648
649 /**
650 * Removes all components from this vector and sets its size to zero.
651 *
652 * <p>This method is identical in functionality to the {@link #clear}
653 * method (which is part of the {@link List} interface).
654 */
655 public synchronized void removeAllElements() {
656 modCount++;
657 // Let gc do its work
658 for (int i = 0; i < elementCount; i++)
659 elementData[i] = null;
660
661 elementCount = 0;
662 }
663
664 /**
665 * Returns a clone of this vector. The copy will contain a
666 * reference to a clone of the internal data array, not a reference
667 * to the original internal data array of this {@code Vector} object.
668 *
669 * @return a clone of this vector
670 */
671 public synchronized Object clone() {
672 try {
673 @SuppressWarnings("unchecked")
674 Vector<E> v = (Vector<E>) super.clone();
675 v.elementData = Arrays.copyOf(elementData, elementCount);
676 v.modCount = 0;
677 return v;
678 } catch (CloneNotSupportedException e) {
679 // this shouldn't happen, since we are Cloneable
680 throw new InternalError(e);
761 * ({@code index < 0 || index >= size()})
762 * @since 1.2
763 */
764 public synchronized E set(int index, E element) {
765 if (index >= elementCount)
766 throw new ArrayIndexOutOfBoundsException(index);
767
768 E oldValue = elementData(index);
769 elementData[index] = element;
770 return oldValue;
771 }
772
773 /**
774 * Appends the specified element to the end of this Vector.
775 *
776 * @param e element to be appended to this Vector
777 * @return {@code true} (as specified by {@link Collection#add})
778 * @since 1.2
779 */
780 public synchronized boolean add(E e) {
781 modCount++;
782 ensureCapacityHelper(elementCount + 1);
783 elementData[elementCount++] = e;
784 return true;
785 }
786
787 /**
788 * Removes the first occurrence of the specified element in this Vector
789 * If the Vector does not contain the element, it is unchanged. More
790 * formally, removes the element with the lowest index i such that
791 * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
792 * an element exists).
793 *
794 * @param o element to be removed from this Vector, if present
795 * @return true if the Vector contained the specified element
796 * @since 1.2
797 */
798 public boolean remove(Object o) {
799 return removeElement(o);
800 }
801
802 /**
862 * specified collection
863 * @throws NullPointerException if the specified collection is null
864 */
865 public synchronized boolean containsAll(Collection<?> c) {
866 return super.containsAll(c);
867 }
868
869 /**
870 * Appends all of the elements in the specified Collection to the end of
871 * this Vector, in the order that they are returned by the specified
872 * Collection's Iterator. The behavior of this operation is undefined if
873 * the specified Collection is modified while the operation is in progress.
874 * (This implies that the behavior of this call is undefined if the
875 * specified Collection is this Vector, and this Vector is nonempty.)
876 *
877 * @param c elements to be inserted into this Vector
878 * @return {@code true} if this Vector changed as a result of the call
879 * @throws NullPointerException if the specified collection is null
880 * @since 1.2
881 */
882 public synchronized boolean addAll(Collection<? extends E> c) {
883 modCount++;
884 Object[] a = c.toArray();
885 int numNew = a.length;
886 ensureCapacityHelper(elementCount + numNew);
887 System.arraycopy(a, 0, elementData, elementCount, numNew);
888 elementCount += numNew;
889 return numNew != 0;
890 }
891
892 /**
893 * Removes from this Vector all of its elements that are contained in the
894 * specified Collection.
895 *
896 * @param c a collection of elements to be removed from the Vector
897 * @return true if this Vector changed as a result of the call
898 * @throws ClassCastException if the types of one or more elements
899 * in this vector are incompatible with the specified
900 * collection
901 * (<a href="Collection.html#optional-restrictions">optional</a>)
902 * @throws NullPointerException if this vector contains one or more null
903 * elements and the specified collection does not support null
904 * elements
905 * (<a href="Collection.html#optional-restrictions">optional</a>),
906 * or if the specified collection is null
907 * @since 1.2
908 */
933 return super.retainAll(c);
934 }
935
936 /**
937 * Inserts all of the elements in the specified Collection into this
938 * Vector at the specified position. Shifts the element currently at
939 * that position (if any) and any subsequent elements to the right
940 * (increases their indices). The new elements will appear in the Vector
941 * in the order that they are returned by the specified Collection's
942 * iterator.
943 *
944 * @param index index at which to insert the first element from the
945 * specified collection
946 * @param c elements to be inserted into this Vector
947 * @return {@code true} if this Vector changed as a result of the call
948 * @throws ArrayIndexOutOfBoundsException if the index is out of range
949 * ({@code index < 0 || index > size()})
950 * @throws NullPointerException if the specified collection is null
951 * @since 1.2
952 */
953 public synchronized boolean addAll(int index, Collection<? extends E> c) {
954 modCount++;
955 if (index < 0 || index > elementCount)
956 throw new ArrayIndexOutOfBoundsException(index);
957
958 Object[] a = c.toArray();
959 int numNew = a.length;
960 ensureCapacityHelper(elementCount + numNew);
961
962 int numMoved = elementCount - index;
963 if (numMoved > 0)
964 System.arraycopy(elementData, index, elementData, index + numNew,
965 numMoved);
966
967 System.arraycopy(a, 0, elementData, index, numNew);
968 elementCount += numNew;
969 return numNew != 0;
970 }
971
972 /**
973 * 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
975 * have the same size, and all corresponding pairs of elements in the two
976 * Lists are <em>equal</em>. (Two elements {@code e1} and
977 * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
978 * e1.equals(e2))}.) In other words, two Lists are defined to be
979 * equal if they contain the same elements in the same order.
980 *
981 * @param o the Object to be compared for equality with this Vector
982 * @return true if the specified Object is equal to this Vector
983 */
984 public synchronized boolean equals(Object o) {
985 return super.equals(o);
986 }
987
988 /**
1030 * @param toIndex high endpoint (exclusive) of the subList
1031 * @return a view of the specified range within this List
1032 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
1033 * {@code (fromIndex < 0 || toIndex > size)}
1034 * @throws IllegalArgumentException if the endpoint indices are out of order
1035 * {@code (fromIndex > toIndex)}
1036 */
1037 public synchronized List<E> subList(int fromIndex, int toIndex) {
1038 return Collections.synchronizedList(super.subList(fromIndex, toIndex),
1039 this);
1040 }
1041
1042 /**
1043 * Removes from this list all of the elements whose index is between
1044 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
1045 * Shifts any succeeding elements to the left (reduces their index).
1046 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
1047 * (If {@code toIndex==fromIndex}, this operation has no effect.)
1048 */
1049 protected synchronized void removeRange(int fromIndex, int toIndex) {
1050 modCount++;
1051 int numMoved = elementCount - toIndex;
1052 System.arraycopy(elementData, toIndex, elementData, fromIndex,
1053 numMoved);
1054
1055 // Let gc do its work
1056 int newElementCount = elementCount - (toIndex-fromIndex);
1057 while (elementCount != newElementCount)
1058 elementData[--elementCount] = null;
1059 }
1060
1061 /**
1062 * Save the state of the {@code Vector} instance to a stream (that
1063 * is, serialize it).
1064 * This method performs synchronization to ensure the consistency
1065 * of the serialized data.
1066 */
1067 private void writeObject(java.io.ObjectOutputStream s)
1068 throws java.io.IOException {
1069 final java.io.ObjectOutputStream.PutField fields = s.putFields();
1070 final Object[] data;
1071 synchronized (this) {
1072 fields.put("capacityIncrement", capacityIncrement);
1073 fields.put("elementCount", elementCount);
1074 data = elementData.clone();
1075 }
|
536 /**
537 * Deletes the component at the specified index. Each component in
538 * this vector with an index greater or equal to the specified
539 * {@code index} is shifted downward to have an index one
540 * smaller than the value it had previously. The size of this vector
541 * is decreased by {@code 1}.
542 *
543 * <p>The index must be a value greater than or equal to {@code 0}
544 * and less than the current size of the vector.
545 *
546 * <p>This method is identical in functionality to the {@link #remove(int)}
547 * method (which is part of the {@link List} interface). Note that the
548 * {@code remove} method returns the old value that was stored at the
549 * specified position.
550 *
551 * @param index the index of the object to remove
552 * @throws ArrayIndexOutOfBoundsException if the index is out of range
553 * ({@code index < 0 || index >= size()})
554 */
555 public synchronized void removeElementAt(int index) {
556 if (index >= elementCount) {
557 throw new ArrayIndexOutOfBoundsException(index + " >= " +
558 elementCount);
559 }
560 else if (index < 0) {
561 throw new ArrayIndexOutOfBoundsException(index);
562 }
563 int j = elementCount - index - 1;
564 if (j > 0) {
565 System.arraycopy(elementData, index + 1, elementData, index, j);
566 }
567 modCount++;
568 elementCount--;
569 elementData[elementCount] = null; /* to let gc do its work */
570 }
571
572 /**
573 * Inserts the specified object as a component in this vector at the
574 * specified {@code index}. Each component in this vector with
575 * an index greater or equal to the specified {@code index} is
576 * shifted upward to have an index one greater than the value it had
577 * previously.
578 *
579 * <p>The index must be a value greater than or equal to {@code 0}
580 * and less than or equal to the current size of the vector. (If the
581 * index is equal to the current size of the vector, the new element
582 * is appended to the Vector.)
583 *
584 * <p>This method is identical in functionality to the
585 * {@link #add(int, Object) add(int, E)}
586 * method (which is part of the {@link List} interface). Note that the
587 * {@code add} method reverses the order of the parameters, to more closely
588 * match array usage.
589 *
590 * @param obj the component to insert
591 * @param index where to insert the new component
592 * @throws ArrayIndexOutOfBoundsException if the index is out of range
593 * ({@code index < 0 || index > size()})
594 */
595 public synchronized void insertElementAt(E obj, int index) {
596 if (index > elementCount) {
597 throw new ArrayIndexOutOfBoundsException(index
598 + " > " + elementCount);
599 }
600 ensureCapacityHelper(elementCount + 1);
601 System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
602 elementData[index] = obj;
603 modCount++;
604 elementCount++;
605 }
606
607 /**
608 * Adds the specified component to the end of this vector,
609 * increasing its size by one. The capacity of this vector is
610 * increased if its size becomes greater than its capacity.
611 *
612 * <p>This method is identical in functionality to the
613 * {@link #add(Object) add(E)}
614 * method (which is part of the {@link List} interface).
615 *
616 * @param obj the component to be added
617 */
618 public synchronized void addElement(E obj) {
619 ensureCapacityHelper(elementCount + 1);
620 modCount++;
621 elementData[elementCount++] = obj;
622 }
623
624 /**
625 * Removes the first (lowest-indexed) occurrence of the argument
626 * from this vector. If the object is found in this vector, each
627 * component in the vector with an index greater or equal to the
628 * object's index is shifted downward to have an index one smaller
629 * than the value it had previously.
630 *
631 * <p>This method is identical in functionality to the
632 * {@link #remove(Object)} method (which is part of the
633 * {@link List} interface).
634 *
635 * @param obj the component to be removed
636 * @return {@code true} if the argument was a component of this
637 * vector; {@code false} otherwise.
638 */
639 public synchronized boolean removeElement(Object obj) {
640 modCount++;
641 int i = indexOf(obj);
642 if (i >= 0) {
643 removeElementAt(i);
644 return true;
645 }
646 return false;
647 }
648
649 /**
650 * Removes all components from this vector and sets its size to zero.
651 *
652 * <p>This method is identical in functionality to the {@link #clear}
653 * method (which is part of the {@link List} interface).
654 */
655 public synchronized void removeAllElements() {
656 // Let gc do its work
657 for (int i = 0; i < elementCount; i++)
658 elementData[i] = null;
659
660 modCount++;
661 elementCount = 0;
662 }
663
664 /**
665 * Returns a clone of this vector. The copy will contain a
666 * reference to a clone of the internal data array, not a reference
667 * to the original internal data array of this {@code Vector} object.
668 *
669 * @return a clone of this vector
670 */
671 public synchronized Object clone() {
672 try {
673 @SuppressWarnings("unchecked")
674 Vector<E> v = (Vector<E>) super.clone();
675 v.elementData = Arrays.copyOf(elementData, elementCount);
676 v.modCount = 0;
677 return v;
678 } catch (CloneNotSupportedException e) {
679 // this shouldn't happen, since we are Cloneable
680 throw new InternalError(e);
761 * ({@code index < 0 || index >= size()})
762 * @since 1.2
763 */
764 public synchronized E set(int index, E element) {
765 if (index >= elementCount)
766 throw new ArrayIndexOutOfBoundsException(index);
767
768 E oldValue = elementData(index);
769 elementData[index] = element;
770 return oldValue;
771 }
772
773 /**
774 * Appends the specified element to the end of this Vector.
775 *
776 * @param e element to be appended to this Vector
777 * @return {@code true} (as specified by {@link Collection#add})
778 * @since 1.2
779 */
780 public synchronized boolean add(E e) {
781 ensureCapacityHelper(elementCount + 1);
782 modCount++;
783 elementData[elementCount++] = e;
784 return true;
785 }
786
787 /**
788 * Removes the first occurrence of the specified element in this Vector
789 * If the Vector does not contain the element, it is unchanged. More
790 * formally, removes the element with the lowest index i such that
791 * {@code (o==null ? get(i)==null : o.equals(get(i)))} (if such
792 * an element exists).
793 *
794 * @param o element to be removed from this Vector, if present
795 * @return true if the Vector contained the specified element
796 * @since 1.2
797 */
798 public boolean remove(Object o) {
799 return removeElement(o);
800 }
801
802 /**
862 * specified collection
863 * @throws NullPointerException if the specified collection is null
864 */
865 public synchronized boolean containsAll(Collection<?> c) {
866 return super.containsAll(c);
867 }
868
869 /**
870 * Appends all of the elements in the specified Collection to the end of
871 * this Vector, in the order that they are returned by the specified
872 * Collection's Iterator. The behavior of this operation is undefined if
873 * the specified Collection is modified while the operation is in progress.
874 * (This implies that the behavior of this call is undefined if the
875 * specified Collection is this Vector, and this Vector is nonempty.)
876 *
877 * @param c elements to be inserted into this Vector
878 * @return {@code true} if this Vector changed as a result of the call
879 * @throws NullPointerException if the specified collection is null
880 * @since 1.2
881 */
882 public boolean addAll(Collection<? extends E> c) {
883 Object[] a = c.toArray();
884 int numNew = a.length;
885 synchronized (this) {
886 ensureCapacityHelper(elementCount + numNew);
887 System.arraycopy(a, 0, elementData, elementCount, numNew);
888 modCount++;
889 elementCount += numNew;
890 }
891 return numNew != 0;
892 }
893
894 /**
895 * Removes from this Vector all of its elements that are contained in the
896 * specified Collection.
897 *
898 * @param c a collection of elements to be removed from the Vector
899 * @return true if this Vector changed as a result of the call
900 * @throws ClassCastException if the types of one or more elements
901 * in this vector are incompatible with the specified
902 * collection
903 * (<a href="Collection.html#optional-restrictions">optional</a>)
904 * @throws NullPointerException if this vector contains one or more null
905 * elements and the specified collection does not support null
906 * elements
907 * (<a href="Collection.html#optional-restrictions">optional</a>),
908 * or if the specified collection is null
909 * @since 1.2
910 */
935 return super.retainAll(c);
936 }
937
938 /**
939 * Inserts all of the elements in the specified Collection into this
940 * Vector at the specified position. Shifts the element currently at
941 * that position (if any) and any subsequent elements to the right
942 * (increases their indices). The new elements will appear in the Vector
943 * in the order that they are returned by the specified Collection's
944 * iterator.
945 *
946 * @param index index at which to insert the first element from the
947 * specified collection
948 * @param c elements to be inserted into this Vector
949 * @return {@code true} if this Vector changed as a result of the call
950 * @throws ArrayIndexOutOfBoundsException if the index is out of range
951 * ({@code index < 0 || index > size()})
952 * @throws NullPointerException if the specified collection is null
953 * @since 1.2
954 */
955 public boolean addAll(int index, Collection<? extends E> c) {
956 if (index < 0 || index > elementCount)
957 throw new ArrayIndexOutOfBoundsException(index);
958
959 Object[] a = c.toArray();
960 int numNew = a.length;
961 synchronized(this) {
962 ensureCapacityHelper(elementCount + numNew);
963
964 int numMoved = elementCount - index;
965 if (numMoved > 0)
966 System.arraycopy(elementData, index, elementData, index + numNew,
967 numMoved);
968
969 System.arraycopy(a, 0, elementData, index, numNew);
970 modCount++;
971 elementCount += numNew;
972 }
973 return numNew != 0;
974 }
975
976 /**
977 * Compares the specified Object with this Vector for equality. Returns
978 * true if and only if the specified Object is also a List, both Lists
979 * have the same size, and all corresponding pairs of elements in the two
980 * Lists are <em>equal</em>. (Two elements {@code e1} and
981 * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
982 * e1.equals(e2))}.) In other words, two Lists are defined to be
983 * equal if they contain the same elements in the same order.
984 *
985 * @param o the Object to be compared for equality with this Vector
986 * @return true if the specified Object is equal to this Vector
987 */
988 public synchronized boolean equals(Object o) {
989 return super.equals(o);
990 }
991
992 /**
1034 * @param toIndex high endpoint (exclusive) of the subList
1035 * @return a view of the specified range within this List
1036 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
1037 * {@code (fromIndex < 0 || toIndex > size)}
1038 * @throws IllegalArgumentException if the endpoint indices are out of order
1039 * {@code (fromIndex > toIndex)}
1040 */
1041 public synchronized List<E> subList(int fromIndex, int toIndex) {
1042 return Collections.synchronizedList(super.subList(fromIndex, toIndex),
1043 this);
1044 }
1045
1046 /**
1047 * Removes from this list all of the elements whose index is between
1048 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
1049 * Shifts any succeeding elements to the left (reduces their index).
1050 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
1051 * (If {@code toIndex==fromIndex}, this operation has no effect.)
1052 */
1053 protected synchronized void removeRange(int fromIndex, int toIndex) {
1054 int numMoved = elementCount - toIndex;
1055 System.arraycopy(elementData, toIndex, elementData, fromIndex,
1056 numMoved);
1057
1058 // Let gc do its work
1059 modCount++;
1060 int newElementCount = elementCount - (toIndex-fromIndex);
1061 while (elementCount != newElementCount)
1062 elementData[--elementCount] = null;
1063 }
1064
1065 /**
1066 * Save the state of the {@code Vector} instance to a stream (that
1067 * is, serialize it).
1068 * This method performs synchronization to ensure the consistency
1069 * of the serialized data.
1070 */
1071 private void writeObject(java.io.ObjectOutputStream s)
1072 throws java.io.IOException {
1073 final java.io.ObjectOutputStream.PutField fields = s.putFields();
1074 final Object[] data;
1075 synchronized (this) {
1076 fields.put("capacityIncrement", capacityIncrement);
1077 fields.put("elementCount", elementCount);
1078 data = elementData.clone();
1079 }
|