Пример #1
0
 /**
  * Appends all of the elements in the specified Collection to the end of this Vector, in the order
  * that they are returned by the specified Collection's Iterator. The behavior of this operation
  * is undefined if the specified Collection is modified while the operation is in progress. (This
  * implies that the behavior of this call is undefined if the specified Collection is this Vector,
  * and this Vector is nonempty.)
  *
  * @param c elements to be inserted into this Vector
  * @return {@code true} if this Vector changed as a result of the call
  * @throws NullPointerException if the specified collection is null
  * @since 1.2
  */
 public synchronized boolean addAll(Collection<? extends E> c) {
   modCount++;
   Object[] a = c.toArray();
   int numNew = a.length;
   ensureCapacityHelper(elementCount + numNew);
   System.arraycopy(a, 0, elementData, elementCount, numNew);
   elementCount += numNew;
   return numNew != 0;
 }
Пример #2
0
 /**
  * Inserts the specified object as a component in this vector at the specified {@code index}. Each
  * component in this vector with an index greater or equal to the specified {@code index} is
  * shifted upward to have an index one greater than the value it had previously.
  *
  * <p>The index must be a value greater than or equal to {@code 0} and less than or equal to the
  * current size of the vector. (If the index is equal to the current size of the vector, the new
  * element is appended to the Vector.)
  *
  * <p>This method is identical in functionality to the {@link #add(int, Object) add(int, E)}
  * method (which is part of the {@link List} interface). Note that the {@code add} method reverses
  * the order of the parameters, to more closely match array usage.
  *
  * @param obj the component to insert
  * @param index where to insert the new component
  * @throws ArrayIndexOutOfBoundsException if the index is out of range ({@code index < 0 || index
  *     > size()})
  */
 public synchronized void insertElementAt(E obj, int index) {
   modCount++;
   if (index > elementCount) {
     throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount);
   }
   ensureCapacityHelper(elementCount + 1);
   System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
   elementData[index] = obj;
   elementCount++;
 }
Пример #3
0
 /**
  * Sets the size of this vector. If the new size is greater than the current size, new {@code
  * null} items are added to the end of the vector. If the new size is less than the current size,
  * all components at index {@code newSize} and greater are discarded.
  *
  * @param newSize the new size of this vector
  * @throws ArrayIndexOutOfBoundsException if the new size is negative
  */
 public synchronized void setSize(int newSize) {
   modCount++;
   if (newSize > elementCount) {
     ensureCapacityHelper(newSize);
   } else {
     for (int i = newSize; i < elementCount; i++) {
       elementData[i] = null;
     }
   }
   elementCount = newSize;
 }
Пример #4
0
  /**
   * Inserts all of the elements in the specified Collection into this Vector at the specified
   * position. Shifts the element currently at that position (if any) and any subsequent elements to
   * the right (increases their indices). The new elements will appear in the Vector in the order
   * that they are returned by the specified Collection's iterator.
   *
   * @param index index at which to insert the first element from the specified collection
   * @param c elements to be inserted into this Vector
   * @return {@code true} if this Vector changed as a result of the call
   * @throws ArrayIndexOutOfBoundsException if the index is out of range ({@code index < 0 || index
   *     > size()})
   * @throws NullPointerException if the specified collection is null
   * @since 1.2
   */
  public synchronized boolean addAll(int index, Collection<? extends E> c) {
    modCount++;
    if (index < 0 || index > elementCount) throw new ArrayIndexOutOfBoundsException(index);

    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacityHelper(elementCount + numNew);

    int numMoved = elementCount - index;
    if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved);

    System.arraycopy(a, 0, elementData, index, numNew);
    elementCount += numNew;
    return numNew != 0;
  }
Пример #5
0
 /**
  * Appends the specified element to the end of this Vector.
  *
  * @param e element to be appended to this Vector
  * @return {@code true} (as specified by {@link Collection#add})
  * @since 1.2
  */
 public synchronized boolean add(E e) {
   modCount++;
   ensureCapacityHelper(elementCount + 1);
   elementData[elementCount++] = e;
   return true;
 }
Пример #6
0
 /**
  * Adds the specified component to the end of this vector, increasing its size by one. The
  * capacity of this vector is increased if its size becomes greater than its capacity.
  *
  * <p>This method is identical in functionality to the {@link #add(Object) add(E)} method (which
  * is part of the {@link List} interface).
  *
  * @param obj the component to be added
  */
 public synchronized void addElement(E obj) {
   modCount++;
   ensureCapacityHelper(elementCount + 1);
   elementData[elementCount++] = obj;
 }
Пример #7
0
 /**
  * Increases the capacity of this vector, if necessary, to ensure that it can hold at least the
  * number of components specified by the minimum capacity argument.
  *
  * <p>If the current capacity of this vector is less than {@code minCapacity}, then its capacity
  * is increased by replacing its internal data array, kept in the field {@code elementData}, with
  * a larger one. The size of the new data array will be the old size plus {@code
  * capacityIncrement}, unless the value of {@code capacityIncrement} is less than or equal to
  * zero, in which case the new capacity will be twice the old capacity; but if this new size is
  * still smaller than {@code minCapacity}, then the new capacity will be {@code minCapacity}.
  *
  * @param minCapacity the desired minimum capacity
  */
 public synchronized void ensureCapacity(int minCapacity) {
   if (minCapacity > 0) {
     modCount++;
     ensureCapacityHelper(minCapacity);
   }
 }