home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / util / Vector.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  5.0 KB  |  228 lines

  1. package java.util;
  2.  
  3. public class Vector implements Cloneable {
  4.    protected Object[] elementData;
  5.    protected int elementCount;
  6.    protected int capacityIncrement;
  7.  
  8.    public Vector(int initialCapacity, int capacityIncrement) {
  9.       this.elementData = new Object[initialCapacity];
  10.       this.capacityIncrement = capacityIncrement;
  11.    }
  12.  
  13.    public Vector(int initialCapacity) {
  14.       this(initialCapacity, 0);
  15.    }
  16.  
  17.    public Vector() {
  18.       this(10);
  19.    }
  20.  
  21.    public final synchronized void copyInto(Object[] anArray) {
  22.       for(int i = this.elementCount; i-- > 0; anArray[i] = this.elementData[i]) {
  23.       }
  24.  
  25.    }
  26.  
  27.    public final synchronized void trimToSize() {
  28.       int oldCapacity = this.elementData.length;
  29.       if (this.elementCount < oldCapacity) {
  30.          Object[] oldData = this.elementData;
  31.          this.elementData = new Object[this.elementCount];
  32.          System.arraycopy(oldData, 0, this.elementData, 0, this.elementCount);
  33.       }
  34.  
  35.    }
  36.  
  37.    public final synchronized void ensureCapacity(int minCapacity) {
  38.       int oldCapacity = this.elementData.length;
  39.       if (minCapacity > oldCapacity) {
  40.          Object[] oldData = this.elementData;
  41.          int newCapacity = this.capacityIncrement > 0 ? oldCapacity + this.capacityIncrement : oldCapacity * 2;
  42.          if (newCapacity < minCapacity) {
  43.             newCapacity = minCapacity;
  44.          }
  45.  
  46.          this.elementData = new Object[newCapacity];
  47.          System.arraycopy(oldData, 0, this.elementData, 0, this.elementCount);
  48.       }
  49.  
  50.    }
  51.  
  52.    public final synchronized void setSize(int newSize) {
  53.       if (newSize > this.elementCount) {
  54.          this.ensureCapacity(newSize);
  55.       } else {
  56.          for(int i = newSize; i < this.elementCount; ++i) {
  57.             this.elementData[i] = null;
  58.          }
  59.       }
  60.  
  61.       this.elementCount = newSize;
  62.    }
  63.  
  64.    public final int capacity() {
  65.       return this.elementData.length;
  66.    }
  67.  
  68.    public final int size() {
  69.       return this.elementCount;
  70.    }
  71.  
  72.    public final boolean isEmpty() {
  73.       return this.elementCount == 0;
  74.    }
  75.  
  76.    public final synchronized Enumeration elements() {
  77.       return new VectorEnumerator(this);
  78.    }
  79.  
  80.    public final boolean contains(Object elem) {
  81.       return this.indexOf(elem, 0) >= 0;
  82.    }
  83.  
  84.    public final int indexOf(Object elem) {
  85.       return this.indexOf(elem, 0);
  86.    }
  87.  
  88.    public final synchronized int indexOf(Object elem, int index) {
  89.       for(int i = index; i < this.elementCount; ++i) {
  90.          if (elem.equals(this.elementData[i])) {
  91.             return i;
  92.          }
  93.       }
  94.  
  95.       return -1;
  96.    }
  97.  
  98.    public final int lastIndexOf(Object elem) {
  99.       return this.lastIndexOf(elem, this.elementCount);
  100.    }
  101.  
  102.    public final synchronized int lastIndexOf(Object elem, int index) {
  103.       int i = index;
  104.  
  105.       do {
  106.          --i;
  107.          if (i < 0) {
  108.             return -1;
  109.          }
  110.       } while(!elem.equals(this.elementData[i]));
  111.  
  112.       return i;
  113.    }
  114.  
  115.    public final synchronized Object elementAt(int index) {
  116.       if (index >= this.elementCount) {
  117.          throw new ArrayIndexOutOfBoundsException(index + " >= " + this.elementCount);
  118.       } else {
  119.          try {
  120.             return this.elementData[index];
  121.          } catch (ArrayIndexOutOfBoundsException var2) {
  122.             throw new ArrayIndexOutOfBoundsException(index + " < 0");
  123.          }
  124.       }
  125.    }
  126.  
  127.    public final synchronized Object firstElement() {
  128.       if (this.elementCount == 0) {
  129.          throw new NoSuchElementException();
  130.       } else {
  131.          return this.elementData[0];
  132.       }
  133.    }
  134.  
  135.    public final synchronized Object lastElement() {
  136.       if (this.elementCount == 0) {
  137.          throw new NoSuchElementException();
  138.       } else {
  139.          return this.elementData[this.elementCount - 1];
  140.       }
  141.    }
  142.  
  143.    public final synchronized void setElementAt(Object obj, int index) {
  144.       if (index >= this.elementCount) {
  145.          throw new ArrayIndexOutOfBoundsException(index + " >= " + this.elementCount);
  146.       } else {
  147.          this.elementData[index] = obj;
  148.       }
  149.    }
  150.  
  151.    public final synchronized void removeElementAt(int index) {
  152.       if (index >= this.elementCount) {
  153.          throw new ArrayIndexOutOfBoundsException(index + " >= " + this.elementCount);
  154.       } else {
  155.          int j = this.elementCount - index - 1;
  156.          if (j > 0) {
  157.             System.arraycopy(this.elementData, index + 1, this.elementData, index, j);
  158.          }
  159.  
  160.          --this.elementCount;
  161.          this.elementData[this.elementCount] = null;
  162.       }
  163.    }
  164.  
  165.    public final synchronized void insertElementAt(Object obj, int index) {
  166.       if (index >= this.elementCount + 1) {
  167.          throw new ArrayIndexOutOfBoundsException(index + " >= " + this.elementCount + 1);
  168.       } else {
  169.          this.ensureCapacity(this.elementCount + 1);
  170.          System.arraycopy(this.elementData, index, this.elementData, index + 1, this.elementCount - index);
  171.          this.elementData[index] = obj;
  172.          ++this.elementCount;
  173.       }
  174.    }
  175.  
  176.    public final synchronized void addElement(Object obj) {
  177.       this.ensureCapacity(this.elementCount + 1);
  178.       this.elementData[this.elementCount++] = obj;
  179.    }
  180.  
  181.    public final synchronized boolean removeElement(Object obj) {
  182.       int i = this.indexOf(obj);
  183.       if (i >= 0) {
  184.          this.removeElementAt(i);
  185.          return true;
  186.       } else {
  187.          return false;
  188.       }
  189.    }
  190.  
  191.    public final synchronized void removeAllElements() {
  192.       for(int i = 0; i < this.elementCount; ++i) {
  193.          this.elementData[i] = null;
  194.       }
  195.  
  196.       this.elementCount = 0;
  197.    }
  198.  
  199.    public synchronized Object clone() {
  200.       try {
  201.          Vector v = (Vector)super.clone();
  202.          v.elementData = new Object[this.elementCount];
  203.          System.arraycopy(this.elementData, 0, v.elementData, 0, this.elementCount);
  204.          return v;
  205.       } catch (CloneNotSupportedException var2) {
  206.          throw new InternalError();
  207.       }
  208.    }
  209.  
  210.    public final synchronized String toString() {
  211.       int max = this.size() - 1;
  212.       StringBuffer buf = new StringBuffer();
  213.       Enumeration e = this.elements();
  214.       buf.append("[");
  215.  
  216.       for(int i = 0; i <= max; ++i) {
  217.          String s = e.nextElement().toString();
  218.          buf.append(s);
  219.          if (i < max) {
  220.             buf.append(", ");
  221.          }
  222.       }
  223.  
  224.       buf.append("]");
  225.       return buf.toString();
  226.    }
  227. }
  228.