home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / Vector.java < prev   
Encoding:
Java Source  |  1998-03-20  |  29.1 KB  |  834 lines

  1. /*
  2.  * @(#)Vector.java    1.54 98/03/18
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util;
  16.  
  17. /**
  18.  * The <code>Vector</code> class implements a growable array of 
  19.  * objects. Like an array, it contains components that can be 
  20.  * accessed using an integer index. However, the size of a 
  21.  * <code>Vector</code> can grow or shrink as needed to accommodate 
  22.  * adding and removing items after the <code>Vector</code> has been created.
  23.  * <p>
  24.  * Each vector tries to optimize storage management by maintaining a 
  25.  * <code>capacity</code> and a <code>capacityIncrement</code>. The 
  26.  * <code>capacity</code> is always at least as large as the vector 
  27.  * size; it is usually larger because as components are added to the 
  28.  * vector, the vector's storage increases in chunks the size of 
  29.  * <code>capacityIncrement</code>. An application can increase the 
  30.  * capacity of a vector before inserting a large number of 
  31.  * components; this reduces the amount of incremental reallocation. 
  32.  * <p>
  33.  * As of JDK1.2, this class has been retrofitted to implement List,
  34.  * so that it becomes a part of Java's collection framework.  Unlike
  35.  * the new collection implementations, Vector is synchronized.
  36.  * <p>
  37.  * The Iterators returned by Vector's iterator and listIterator
  38.  * methods are <em>fail-fast</em>: if the Vector is structurally modified
  39.  * at any time after the Iterator is created, in any way except through the
  40.  * Iterator's own remove or add methods, the Iterator will throw a
  41.  * ConcurrentModificationException.  Thus, in the face of concurrent
  42.  * modification, the Iterator fails quickly and cleanly, rather than risking
  43.  * arbitrary, non-deterministic behavior at an undetermined time in the future.
  44.  * The Enumerations returned by Vector's elements method are <em>not</em>
  45.  * fail-fast.
  46.  *
  47.  * @author  Lee Boynton
  48.  * @author  Jonathan Payne
  49.  * @version 1.54, 03/18/98
  50.  * @see Collection
  51.  * @see List
  52.  * @see ArrayList
  53.  * @see LinkedList
  54.  * @since   JDK1.0
  55.  */
  56. public class Vector extends AbstractList implements List, Cloneable,
  57.                                 java.io.Serializable {
  58.     /**
  59.      * The array buffer into which the components of the vector are 
  60.      * stored. The capacity of the vector is the length of this array buffer, 
  61.      * and is at least large enough to contain all the vector's elements.
  62.      */
  63.     protected Object elementData[];
  64.  
  65.     /**
  66.      * The number of valid components in this <tt>Vector</tt> object. 
  67.      * Components <tt>elementData[0]</tt> through 
  68.      * <tt>elementData[elementCount-1]</tt> are the actual items.
  69.      */
  70.     protected int elementCount;
  71.  
  72.     /**
  73.      * The amount by which the capacity of the vector is automatically 
  74.      * incremented when its size becomes greater than its capacity. If 
  75.      * the capacity increment is <code>0</code>, the capacity of the 
  76.      * vector is doubled each time it needs to grow. 
  77.      */
  78.     protected int capacityIncrement;
  79.  
  80.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  81.     private static final long serialVersionUID = -2767605614048989439L;
  82.  
  83.     /**
  84.      * Constructs an empty vector with the specified initial capacity and
  85.      * capacity increment. 
  86.      *
  87.      * @param   initialCapacity     the initial capacity of the vector.
  88.      * @param   capacityIncrement   the amount by which the capacity is
  89.      *                              increased when the vector overflows.
  90.      * @exception IllegalArgumentException if the specified initial capacity
  91.      *               is negative
  92.      */
  93.     public Vector(int initialCapacity, int capacityIncrement) {
  94.     super();
  95.         if (initialCapacity < 0)
  96.             throw new IllegalArgumentException("Illegal Capacity: "+
  97.                                                initialCapacity);
  98.     this.elementData = new Object[initialCapacity];
  99.     this.capacityIncrement = capacityIncrement;
  100.     }
  101.  
  102.     /**
  103.      * Constructs an empty vector with the specified initial capacity and 
  104.      * with its capacity increment equal to zero.
  105.      *
  106.      * @param   initialCapacity   the initial capacity of the vector.
  107.      * @exception IllegalArgumentException if the specified initial capacity
  108.      *               is negative
  109.      */
  110.     public Vector(int initialCapacity) {
  111.     this(initialCapacity, 0);
  112.     }
  113.  
  114.     /**
  115.      * Constructs an empty vector so that its internal data array 
  116.      * has size <tt>10</tt> and its standard capacity increment is 
  117.      * zero. 
  118.      */
  119.     public Vector() {
  120.     this(10);
  121.     }
  122.  
  123.     /**
  124.      * Constructs a vector containing the elements of the specified
  125.      * collection, in the order they are returned by the collection's
  126.      * iterator.
  127.      *
  128.      * @since   JDK1.2
  129.      */
  130.     public Vector(Collection c) {
  131.     this((c.size()*110)/100);    // Allow 10% room for growth
  132.  
  133.     Iterator i = c.iterator();
  134.     while (i.hasNext())
  135.         elementData[elementCount++] = i.next();
  136.     }
  137.  
  138.     /**
  139.      * Copies the components of this vector into the specified array. The 
  140.      * item at index <tt>k</tt> in this vector is copied into component 
  141.      * <tt>k</tt> of <tt>anArray</tt>. The array must be big enough to hold 
  142.      * all the objects in this vector, else an 
  143.      * <tt>IndexOutOfBoundsException</tt> is thrown.
  144.      *
  145.      * @param   anArray   the array into which the components get copied.
  146.      */
  147.     public synchronized void copyInto(Object anArray[]) {
  148.     System.arraycopy(elementData, 0, anArray, 0, elementCount);
  149.     }
  150.  
  151.     /**
  152.      * Trims the capacity of this vector to be the vector's current 
  153.      * size. If the capacity of this cector is larger than its current 
  154.      * size, then the capacity is changed to equal the size by replacing 
  155.      * its internal data array, kept in the field <tt>elementData</tt>, 
  156.      * with a smaller one. An application can use this operation to 
  157.      * minimize the storage of a vector. 
  158.      */
  159.     public synchronized void trimToSize() {
  160.     modCount++;
  161.     int oldCapacity = elementData.length;
  162.     if (elementCount < oldCapacity) {
  163.         Object oldData[] = elementData;
  164.         elementData = new Object[elementCount];
  165.         System.arraycopy(oldData, 0, elementData, 0, elementCount);
  166.     }
  167.     }
  168.  
  169.     /**
  170.      * Increases the capacity of this vector, if necessary, to ensure 
  171.      * that it can hold at least the number of components specified by 
  172.      * the minimum capacity argument. <p>If the current capacity of this 
  173.      * vector is less than <tt>minCapacity</tt>, then its capacity is
  174.      * increased by replacing its internal data array, kept in the field 
  175.      * <tt>elementData</tt>, with a larger one. The size of the new data 
  176.      * array will be the old size plus <tt>capacityIncrement</tt>, unless 
  177.      * the value of <tt>capacityIncrement</tt> is nonpositive, in which 
  178.      * case the new capacity will be twice the old capacity; but if
  179.      * this new size is still smaller than <tt>minCapacity</tt>, then the 
  180.      * new capacity will be <tt>minCapacity</tt>. 
  181.      *
  182.      * @param   minCapacity   the desired minimum capacity.
  183.      */
  184.     public synchronized void ensureCapacity(int minCapacity) {
  185.     ensureCapacityHelper(minCapacity);
  186.     }
  187.     
  188.     /**
  189.      * This implements the unsynchronized semantics of ensureCapacity.
  190.      * Synchronized methods in this class can internally call this 
  191.      * method for ensuring capacity without incurring the cost of an 
  192.      * extra synchronization.
  193.      *
  194.      * @see java.util.Vector#ensureCapacity(int)
  195.      */ 
  196.     private void ensureCapacityHelper(int minCapacity) {
  197.     modCount++;
  198.     int oldCapacity = elementData.length;
  199.     if (minCapacity > oldCapacity) {
  200.         Object oldData[] = elementData;
  201.         int newCapacity = (capacityIncrement > 0) ?
  202.         (oldCapacity + capacityIncrement) : (oldCapacity * 2);
  203.             if (newCapacity < minCapacity) {
  204.         newCapacity = minCapacity;
  205.         }
  206.         elementData = new Object[newCapacity];
  207.         System.arraycopy(oldData, 0, elementData, 0, elementCount);
  208.     }
  209.     }
  210.  
  211.     /**
  212.      * Sets the size of this vector. If the new size is greater than the 
  213.      * current size, new <code>null</code> items are added to the end of 
  214.      * the vector. If the new size is less than the current size, all 
  215.      * components at index <code>newSize</code> and greater are discarded.
  216.      *
  217.      * @param   newSize   the new size of this vector.
  218.      */
  219.     public synchronized void setSize(int newSize) {
  220.     modCount++;
  221.     if (newSize > elementCount) {
  222.         ensureCapacityHelper(newSize);
  223.     } else {
  224.         for (int i = newSize ; i < elementCount ; i++) {
  225.         elementData[i] = null;
  226.         }
  227.     }
  228.     elementCount = newSize;
  229.     }
  230.  
  231.     /**
  232.      * Returns the current capacity of this vector.
  233.      *
  234.      * @return  the current capacity (the length of its internal 
  235.      *          data arary, kept in the field <tt>elementData</tt> 
  236.      *          of this vector.
  237.      */
  238.     public int capacity() {
  239.     return elementData.length;
  240.     }
  241.  
  242.     /**
  243.      * Returns the number of components in this vector.
  244.      *
  245.      * @return  the number of components in this vector.
  246.      */
  247.     public int size() {
  248.     return elementCount;
  249.     }
  250.  
  251.     /**
  252.      * Tests if this vector has no components.
  253.      *
  254.      * @return  <code>true</code> if and only if this vector has 
  255.      *          no components, that is, its size is zero;
  256.      *          <code>false</code> otherwise.
  257.      */
  258.     public boolean isEmpty() {
  259.     return elementCount == 0;
  260.     }
  261.  
  262.     /**
  263.      * Returns an enumeration of the components of this vector. The 
  264.      * returned <tt>Enumeration</tt> object will generate all items in 
  265.      * this vector. The first item generated is the item at index <tt>0</tt>, 
  266.      * then the item at index <tt>1</tt>, and so on. 
  267.      *
  268.      * @return  an enumeration of the components of this vector.
  269.      * @see     Enumeration
  270.      * @see     Iterator
  271.      */
  272.     public synchronized Enumeration elements() {
  273.     return new Enumeration() {
  274.         int count = 0;
  275.  
  276.         public boolean hasMoreElements() {
  277.         return count < elementCount;
  278.         }
  279.  
  280.         public Object nextElement() {
  281.         synchronized (Vector.this) {
  282.             if (count < elementCount) {
  283.             return elementData[count++];
  284.             }
  285.         }
  286.         throw new NoSuchElementException("Vector Enumeration");
  287.         }
  288.     };
  289.     }
  290.  
  291.     /**
  292.      * Tests if the specified object is a component in this vector.
  293.      *
  294.      * @param   elem   an object.
  295.      * @return  <code>true</code> if and only if the specified object 
  296.      * is the same as a component in this vector, as determined by the 
  297.      * <tt>equals</tt> method; <code>false</code> otherwise.
  298.      */
  299.     public boolean contains(Object elem) {
  300.     return indexOf(elem, 0) >= 0;
  301.     }
  302.  
  303.     /**
  304.      * Searches for the first occurence of the given argument, testing 
  305.      * for equality using the <code>equals</code> method. 
  306.      *
  307.      * @param   elem   an object.
  308.      * @return  the index of the first occurrence of the argument in this
  309.      *          vector, that is, the smallest value <tt>k</tt> such that 
  310.      *          <tt>elem.equals(elementData[k])</tt> is <tt>true</tt>; 
  311.      *          returns <code>-1</code> if the object is not found.
  312.      * @see     Object#equals(Object)
  313.      */
  314.     public int indexOf(Object elem) {
  315.     return indexOf(elem, 0);
  316.     }
  317.  
  318.     /**
  319.      * Searches for the first occurence of the given argument, beginning 
  320.      * the search at <code>index</code>, and testing for equality using 
  321.      * the <code>equals</code> method. 
  322.      *
  323.      * @param   elem    an object.
  324.      * @param   index   the index to start searching from.
  325.      * @return  the index of the first occurrence of the object argument in
  326.      *          this vector at position <code>index</code> or later in the
  327.      *          vector, that is, the smallest value <tt>k</tt> such that 
  328.      *          <tt>elem.equals(elementData[k]) && (k >= index)</tt> is 
  329.      *          <tt>true</tt>; returns <code>-1</code> if the object is not 
  330.      *           found.
  331.      * @see     Object#equals(Object)
  332.      */
  333.     public synchronized int indexOf(Object elem, int index) {
  334.     if (elem == null) {
  335.         for (int i = index ; i < elementCount ; i++)
  336.         if (elementData[i]==null)
  337.             return i;
  338.     } else {
  339.         for (int i = index ; i < elementCount ; i++)
  340.         if (elem.equals(elementData[i]))
  341.             return i;
  342.     }
  343.     return -1;
  344.     }
  345.  
  346.     /**
  347.      * Returns the index of the last occurrence of the specified object in
  348.      * this vector.
  349.      *
  350.      * @param   elem   the desired component.
  351.      * @return  the index of the last occurrence of the specified object in
  352.      *          this vector, that is, the largest value <tt>k</tt> such that 
  353.      *          <tt>elem.equals(elementData[k])</tt> is <tt>true</tt>; 
  354.      *          returns <code>-1</code> if the object is not found.
  355.      */
  356.     public int lastIndexOf(Object elem) {
  357.     return lastIndexOf(elem, elementCount-1);
  358.     }
  359.  
  360.     /**
  361.      * Searches backwards for the specified object, starting from the 
  362.      * specified index, and returns an index to it. 
  363.      *
  364.      * @param  elem    the desired component.
  365.      * @param  index   the index to start searching from.
  366.      * @return the index of the last occurrence of the specified object in this
  367.      *          vector at position less than <code>index</code> in the 
  368.      *          vector, that is, the largest value <tt>k</tt> such that 
  369.      *          <tt>elem.equals(elementData[k]) && (k <= index)</tt> is 
  370.      *          <tt>true</tt>; <code>-1</code> if the object is not found.
  371.      */
  372.     public synchronized int lastIndexOf(Object elem, int index) {
  373.     if (elem == null) {
  374.         for (int i = index; i >= 0; i--)
  375.         if (elementData[i]==null)
  376.             return i;
  377.     } else {
  378.         for (int i = index; i >= 0; i--)
  379.         if (elem.equals(elementData[i]))
  380.             return i;
  381.     }
  382.     return -1;
  383.     }
  384.  
  385.     /**
  386.      * Returns the component at the specified index.
  387.      * <p>
  388.      * This method is identical in functionality to the get method
  389.      * (which is part of the List interface).
  390.      *
  391.      * @param      index   an index into this vector.
  392.      * @return     the component at the specified index.
  393.      * @exception  ArrayIndexOutOfBoundsException  if the <tt>index</tt> 
  394.      *             is negative or not less than the current size of this 
  395.      *             <tt>Vector</tt> object.
  396.      *             given.
  397.      * @see       #get(int)
  398.      * @see       List
  399.      */
  400.     public synchronized Object elementAt(int index) {
  401.     if (index >= elementCount) {
  402.         throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
  403.     }
  404.     /* Since try/catch is free, except when the exception is thrown,
  405.        put in this extra try/catch to catch negative indexes and
  406.        display a more informative error message.  This might not
  407.        be appropriate, especially if we have a decent debugging
  408.        environment - JP. */
  409.     try {
  410.         return elementData[index];
  411.     } catch (ArrayIndexOutOfBoundsException e) {
  412.         throw new ArrayIndexOutOfBoundsException(index + " < 0");
  413.     }
  414.     }
  415.  
  416.     /**
  417.      * Returns the first component (the item at index <tt>0</tt>) of 
  418.      * this vector.
  419.      *
  420.      * @return     the first component of this vector.
  421.      * @exception  NoSuchElementException  if this vector has no components.
  422.      */
  423.     public synchronized Object firstElement() {
  424.     if (elementCount == 0) {
  425.         throw new NoSuchElementException();
  426.     }
  427.     return elementData[0];
  428.     }
  429.  
  430.     /**
  431.      * Returns the last component of the vector.
  432.      *
  433.      * @return  the last component of the vector, i.e., the component at index
  434.      *          <code>size() - 1</code>.
  435.      * @exception  NoSuchElementException  if this vector is empty.
  436.      */
  437.     public synchronized Object lastElement() {
  438.     if (elementCount == 0) {
  439.         throw new NoSuchElementException();
  440.     }
  441.     return elementData[elementCount - 1];
  442.     }
  443.  
  444.     /**
  445.      * Sets the component at the specified <code>index</code> of this 
  446.      * vector to be the specified object. The previous component at that 
  447.      * position is discarded.
  448.      * <p>
  449.      * The index must be a value greater than or equal to <code>0</code> 
  450.      * and less than the current size of the vector. 
  451.      * <p>
  452.      * This method is identical in functionality to the set method
  453.      * (which is part of the List interface). Note that the set method reverses
  454.      * the order of the parameters, to more closely match array usage.  Note
  455.      * also that the set method returns the old value that was stored at the
  456.      * specified position.
  457.      *
  458.      * @param      obj     what the component is to be set to.
  459.      * @param      index   the specified index.
  460.      * @exception  ArrayIndexOutOfBoundsException  if the index was invalid.
  461.      * @see        #size()
  462.      * @see        List
  463.      * @see       set
  464.      */
  465.     public synchronized void setElementAt(Object obj, int index) {
  466.     if (index >= elementCount) {
  467.         throw new ArrayIndexOutOfBoundsException(index + " >= " + 
  468.                              elementCount);
  469.     }
  470.     elementData[index] = obj;
  471.     }
  472.  
  473.     /**
  474.      * Deletes the component at the specified index. Each component in 
  475.      * this vector with an index greater or equal to the specified 
  476.      * <code>index</code> is shifted downward to have an index one 
  477.      * smaller than the value it had previously. The size of this vector 
  478.      * is decreased by <tt>1</tt>.
  479.      * <p>
  480.      * The index must be a value greater than or equal to <code>0</code> 
  481.      * and less than the current size of the vector. 
  482.      * <p>
  483.      * This method is identical in functionality to the remove method
  484.      * (which is part of the List interface).  Note that the remove method
  485.      * returns the old value that was stored at the specified position.
  486.      *
  487.      * @param      index   the index of the object to remove.
  488.      * @exception  ArrayIndexOutOfBoundsException  if the index was invalid.
  489.      * @see        #size()
  490.      * @see       #remove(int)
  491.      * @see       List
  492.      */
  493.     public synchronized void removeElementAt(int index) {
  494.     modCount++;
  495.     if (index >= elementCount) {
  496.         throw new ArrayIndexOutOfBoundsException(index + " >= " + 
  497.                              elementCount);
  498.     }
  499.     else if (index < 0) {
  500.         throw new ArrayIndexOutOfBoundsException(index);
  501.     }
  502.     int j = elementCount - index - 1;
  503.     if (j > 0) {
  504.         System.arraycopy(elementData, index + 1, elementData, index, j);
  505.     }
  506.     elementCount--;
  507.     elementData[elementCount] = null; /* to let gc do its work */
  508.     }
  509.  
  510.     /**
  511.      * Inserts the specified object as a component in this vector at the 
  512.      * specified <code>index</code>. Each component in this vector with 
  513.      * an index greater or equal to the specified <code>index</code> is 
  514.      * shifted upward to have an index one greater than the value it had 
  515.      * previously. 
  516.      * <p>
  517.      * The index must be a value greater than or equal to <code>0</code> 
  518.      * and less than or equal to the current size of the vector. 
  519.      * <p>
  520.      * This method is identical in functionality to the add(Object, int) method
  521.      * (which is part of the List interface). Note that the add method reverses
  522.      * the order of the parameters, to more closely match array usage.
  523.      *
  524.      * @param      obj     the component to insert.
  525.      * @param      index   where to insert the new component.
  526.      * @exception  ArrayIndexOutOfBoundsException  if the index was invalid.
  527.      * @see        #size()
  528.      * @see       #add(Object, int)
  529.      * @see       List
  530.      */
  531.     public synchronized void insertElementAt(Object obj, int index) {
  532.     modCount++;
  533.     if (index >= elementCount + 1) {
  534.         throw new ArrayIndexOutOfBoundsException(index
  535.                              + " > " + elementCount);
  536.     }
  537.     ensureCapacityHelper(elementCount + 1);
  538.     System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
  539.     elementData[index] = obj;
  540.     elementCount++;
  541.     }
  542.  
  543.     /**
  544.      * Adds the specified component to the end of this vector, 
  545.      * increasing its size by one. The capacity of this vector is 
  546.      * increased if its size becomes greater than its capacity. 
  547.      * <p>
  548.      * This method is identical in functionality to the add(Object) method
  549.      * (which is part of the List interface).
  550.      *
  551.      * @param   obj   the component to be added.
  552.      * @see       #add(Object)
  553.      * @see       List
  554.      */
  555.     public synchronized void addElement(Object obj) {
  556.     modCount++;
  557.     ensureCapacityHelper(elementCount + 1);
  558.     elementData[elementCount++] = obj;
  559.     }
  560.  
  561.     /**
  562.      * Removes the first (lowest-indexed) occurrence of the argument 
  563.      * from this vector. If the object is found in this vector, each 
  564.      * component in the vector with an index greater or equal to the 
  565.      * object's index is shifted downward to have an index one smaller 
  566.      * than the value it had previously.
  567.      * <p>
  568.      * This method is identical in functionality to the remove(Object) 
  569.      * method (which is part of the List interface).
  570.      *
  571.      * @param   obj   the component to be removed.
  572.      * @return  <code>true</code> if the argument was a component of this
  573.      *          vector; <code>false</code> otherwise.
  574.      * @see    List#remove(Object)
  575.      * @see    List
  576.      */
  577.     public synchronized boolean removeElement(Object obj) {
  578.     modCount++;
  579.     int i = indexOf(obj);
  580.     if (i >= 0) {
  581.         removeElementAt(i);
  582.         return true;
  583.     }
  584.     return false;
  585.     }
  586.  
  587.     /**
  588.      * Removes all components from this vector and sets its size to zero.
  589.      * <p>
  590.      * This method is identical in functionality to the clear method
  591.      * (which is part of the List interface).
  592.      *
  593.      * @see    clear
  594.      * @see    List
  595.      */
  596.     public synchronized void removeAllElements() {
  597.     modCount++;
  598.     for (int i = 0; i < elementCount; i++) {
  599.         elementData[i] = null;
  600.     }
  601.     elementCount = 0;
  602.     }
  603.  
  604.     /**
  605.      * Returns a clone of this vector. The copy will contain a
  606.      * reference to a clone of the internal data array, not a reference 
  607.      * to the original internal data array of this <tt>Vector</tt> object. 
  608.      *
  609.      * @return  a clone of this vector.
  610.      */
  611.     public synchronized Object clone() {
  612.     try { 
  613.         Vector v = (Vector)super.clone();
  614.         v.elementData = new Object[elementCount];
  615.         System.arraycopy(elementData, 0, v.elementData, 0, elementCount);
  616.         v.modCount = 0;
  617.         return v;
  618.     } catch (CloneNotSupportedException e) { 
  619.         // this shouldn't happen, since we are Cloneable
  620.         throw new InternalError();
  621.     }
  622.     }
  623.  
  624.     /**
  625.      * Returns an array containing all of the elements in this Vector
  626.      * in the correct order.
  627.      *
  628.      * @since JDK1.2
  629.      */
  630.     public synchronized Object[] toArray() {
  631.     Object[] result = new Object[elementCount];
  632.     System.arraycopy(elementData, 0, result, 0, elementCount);
  633.     return result;
  634.     }
  635.  
  636.  
  637.     // Positional Access Operations
  638.  
  639.     /**
  640.      * Returns the element at the specified position in this Vector.
  641.      *
  642.      * @param index index of element to return.
  643.      * @exception ArrayIndexOutOfBoundsException index is out of range (index
  644.      *           < 0 || index >= size()).
  645.      * @since JDK1.2
  646.      */
  647.     public synchronized Object get(int index) {
  648.     if (index >= elementCount)
  649.         throw new ArrayIndexOutOfBoundsException(index);
  650.  
  651.     return elementData[index];
  652.     }
  653.  
  654.     /**
  655.      * Replaces the element at the specified position in this Vector with the
  656.      * specified element.
  657.      *
  658.      * @param index index of element to replace.
  659.      * @param element element to be stored at the specified position.
  660.      * @return the element previously at the specified position.
  661.      * @exception ArrayIndexOutOfBoundsException index out of range
  662.      *          (index < 0 || index >= size()).
  663.      * @exception IllegalArgumentException fromIndex > toIndex.
  664.      * @since JDK1.2
  665.      */
  666.     public synchronized Object set(int index, Object element) {
  667.     if (index >= elementCount)
  668.         throw new ArrayIndexOutOfBoundsException(index);
  669.  
  670.     Object oldValue = elementData[index];
  671.     elementData[index] = element;
  672.     return oldValue;
  673.     }
  674.  
  675.     /**
  676.      * Inserts the specified element at the specified position in this Vector.
  677.      * Shifts the element currently at that position (if any) and any
  678.      * subsequent elements to the right (adds one to their indices).
  679.      *
  680.      * @param index index at which the specified element is to be inserted.
  681.      * @param element element to be inserted.
  682.      * @exception ArrayIndexOutOfBoundsException index is out of range
  683.      *          (index < 0 || index > size()).
  684.      * @since JDK1.2
  685.      */
  686.     public synchronized void add(int index, Object element) {
  687.     if (index > elementCount)
  688.         throw new ArrayIndexOutOfBoundsException(index);
  689.  
  690.     ensureCapacityHelper(elementCount+1);     // Increments modCount
  691.     System.arraycopy(elementData, index, elementData, index + 1,
  692.              elementCount - index);
  693.     elementData[index] = element;
  694.     elementCount++;
  695.     }
  696.  
  697.     /**
  698.      * Removes the element at the specified position in this Vector.
  699.      * shifts any subsequent elements to the left (subtracts one from their
  700.      * indices).  Returns the element that was removed from the Vector.
  701.      *
  702.      * @exception ArrayIndexOutOfBoundsException index out of range (index
  703.      *           < 0 || index >= size()).
  704.      * @param index the index of the element to removed.
  705.      * @since JDK1.2
  706.      */
  707.     public synchronized Object remove(int index) {
  708.     modCount++;
  709.     if (index >= elementCount)
  710.         throw new ArrayIndexOutOfBoundsException(index);
  711.     Object oldValue = elementData[index];
  712.  
  713.     int numMoved = elementCount - index - 1;
  714.     if (numMoved > 0)
  715.         System.arraycopy(elementData, index+1, elementData, index,
  716.                  numMoved);
  717.     elementData[--elementCount] = null; // Let gc do its work
  718.  
  719.     return oldValue;
  720.     }
  721.  
  722.     /**
  723.      * Removes all of the elements from this Vector.  The Vector will
  724.      * be empty after this call returns (unless it throws an exception).
  725.      *
  726.      * @exception UnsupportedOperationException clear is not supported
  727.      *           by this Set.
  728.      * @since JDK1.2
  729.      */
  730.     public synchronized void clear() {
  731.     modCount++;
  732.  
  733.     // Let gc do its work
  734.     for (int i = 0; i < elementCount; i++)
  735.         elementData[i] = null;
  736.  
  737.     elementCount = 0;
  738.     }
  739.  
  740.     /**
  741.      * Appends all of the elements in the specified Collection to the end of
  742.      * this this Vector, in the order that they are returned by the specified
  743.      * Collection's Iterator.  The behavior of this operation is undefined if
  744.      * the specified Collection is modified while the operation is in progress.
  745.      * (This implies that the behavior of this call is undefined if the the
  746.      * specified Collection is this Vector, and this Vector is nonempty.)
  747.      *
  748.      * @param index index at which to insert first element
  749.      *              from the specified collection.
  750.      * @param c elements to be inserted into this Vector.
  751.      * @exception ArrayIndexOutOfBoundsException index out of range (index
  752.      *          < 0 || index > size()).
  753.      * @since JDK1.2
  754.      */
  755.     public synchronized boolean addAll(Collection c) {
  756.     modCount++;
  757.     int numNew = c.size();
  758.     ensureCapacityHelper(elementCount + numNew);
  759.  
  760.     Iterator e = c.iterator();
  761.     for (int i=0; i<numNew; i++)
  762.         elementData[elementCount++] = e.next();
  763.  
  764.     return numNew != 0;
  765.     }
  766.  
  767.     /**
  768.      * Removes from this Vector all of the elements whose index is between
  769.      * fromIndex, inclusive and toIndex, exclusive.  Shifts any succeeding
  770.      * elements to the left (reduces their index).
  771.      * This call shortens the Vector by (toIndex - fromIndex) elements.  (If
  772.      * toIndex==fromIndex, this operation has no effect.)
  773.      *
  774.      * @param fromIndex index of first element to be removed.
  775.      * @param fromIndex index after last element to be removed.
  776.      * @exception ArrayIndexOutOfBoundsException fromIndex or toIndex out of
  777.      *          range (fromIndex < 0 || fromIndex >= size() || toIndex
  778.      *          > size() || toIndex < fromIndex).
  779.      * @since JDK1.2
  780.      */
  781.     public synchronized void removeRange(int fromIndex, int toIndex) {
  782.     modCount++;
  783.     if (fromIndex < 0 || fromIndex >= elementCount ||
  784.         toIndex > elementCount || toIndex < fromIndex)
  785.         throw new ArrayIndexOutOfBoundsException();
  786.  
  787.     int numMoved = elementCount - toIndex;
  788.     if (numMoved > 0)
  789.         System.arraycopy(elementData, toIndex, elementData, fromIndex,
  790.                  numMoved);
  791.  
  792.     // Let gc do its work
  793.     int newElementCount = elementCount - (toIndex-fromIndex);
  794.     while (elementCount != newElementCount)
  795.         elementData[--elementCount] = null;
  796.     }
  797.  
  798.     /**
  799.      * Inserts all of the elements in in the specified Collection into this
  800.      * Vector at the specified position.  Shifts the element currently at
  801.      * that position (if any) and any subsequent elements to the right
  802.      * (increases their indices).  The new elements will appear in the Vector  
  803.      * in the order that they are returned by the specified Collection's
  804.      * iterator.
  805.      *
  806.      * @param index index at which to insert first element
  807.      *            from the specified collection.
  808.      * @param c elements to be inserted into this Vector.
  809.      * @exception ArrayIndexOutOfBoundsException index out of range (index
  810.      *          < 0 || index > size()).
  811.      * @since JDK1.2
  812.      */
  813.     public synchronized boolean addAll(int index, Collection c) {
  814.     modCount++;
  815.     if (index < 0 || index > elementCount)
  816.         throw new ArrayIndexOutOfBoundsException(index);        
  817.  
  818.     int numNew = c.size();
  819.     ensureCapacityHelper(elementCount + numNew);
  820.  
  821.     int numMoved = elementCount - index;
  822.     if (numMoved > 0)
  823.         System.arraycopy(elementData, index, elementData, index + numNew,
  824.                  numMoved);
  825.  
  826.     Iterator e = c.iterator();
  827.     for (int i=0; i<numNew; i++)
  828.         elementData[index++] = e.next();
  829.  
  830.     elementCount += numNew;
  831.     return numNew != 0;
  832.     }
  833. }
  834.