Javaベース:Vector


1.概要
VectorはAbstractList抽象クラスを継承し、List,RandomAccess,Cloneable,javaを実現する.io.SerializableインタフェースVectorはAbstractList抽象クラスを継承し、List,RandomAccess,Cloneable,javaを実現する.io.Serializableインタフェース
  • Listインタフェース:Listの操作規範
  • が制定する
  • RandomAccessインタフェース:ランダムアクセス
  • Cloneableインタフェース:オブジェクト(浅いコピー)
  • をクローンできます.
  • Serializableインタフェース:オブジェクトシーケンス
  • Vectorは本質的にスレッドセキュリティの動的拡張可能な配列であり、ArrayListが非スレッドセキュリティの動的拡張可能な配列であることとは異なり、両者のAPIは基本的に同じであり、Vector拡張は各拡張後の新容量=2*旧容量をカスタマイズまたはデフォルトで設定することができる.
    2.メンバー属性
     //  vector      
     protected Object[] elementData;
    
     //vector        
     protected int elementCount;
     
     //vector            
     // vector     elementCount           ,vector       
     //  capacityIncrement     0,vector              
     protected int capacityIncrement;
    
     //     
     private static final long serialVersionUID = -2767605614048989439L;
    

    3.コンストラクタ
        /**
         *          initialCapacity、     capacityIncrement  vector
         * @param   initialCapacity          
         * @param   capacityIncrement       
         * @throws IllegalArgumentException   initialCapacity   
         */
    public Vector(int initialCapacity, int capacityIncrement) {
         
            super();
            if (initialCapacity < 0)
                throw new IllegalArgumentException("Illegal Capacity: "+
                                                   initialCapacity);
            this.elementData = new Object[initialCapacity];
            this.capacityIncrement = capacityIncrement;
        }
    
        /**
         *          initialCapacity、     0  vector
         * @param   initialCapacity        
         * @throws IllegalArgumentException   initialCapacity   
         */
        public Vector(int initialCapacity) {
         
            this(initialCapacity, 0);
        }
    
        /**
         *          10、     0  vector
         */
        public Vector() {
         
            this(10);
        }
    
        /**
         *         Collection      vector
         * @param c     vector Collection
         * @throws NullPointerException     vector Collection null
         */
        public Vector(Collection<? extends E> c) {
         
            elementData = c.toArray();
            elementCount = elementData.length;
            // c.toArray might (incorrectly) not return Object[] (see 6260652)
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
        }
    

    4.その他の方法
        //    Vector           anArray     
        public synchronized void copyInto(Object[] anArray) {
             
            System.arraycopy(elementData, 0, anArray, 0, elementCount);    
        }    
       
        //                   
        public synchronized void trimToSize() {
             
            modCount++;    
            int oldCapacity = elementData.length;    
            if (elementCount < oldCapacity) {
             
                elementData = Arrays.copyOf(elementData, elementCount);    
            }    
        }    
       
        //   “Vector  ”         
        private void ensureCapacityHelper(int minCapacity) {
             
            int oldCapacity = elementData.length;    
            //  Vector               ,      。    
            //         >0( capacityIncrement>0),       capacityIncrement    
            //   ,       。    
            if (minCapacity > oldCapacity) {
             
                Object[] oldData = elementData;    
                int newCapacity = (capacityIncrement > 0) ?    
                    (oldCapacity + capacityIncrement) : (oldCapacity * 2);    
                if (newCapacity < minCapacity) {
             
                    newCapacity = minCapacity;    
                }    
                elementData = Arrays.copyOf(elementData, newCapacity);    
            }    
        }    
       
        //   Vector   。    
        public synchronized void ensureCapacity(int minCapacity) {
             
            //  Vector      +1    
            modCount++;    
            ensureCapacityHelper(minCapacity);    
        }    
       
        //        newSize    
        public synchronized void setSize(int newSize) {
             
            modCount++;    
            if (newSize > elementCount) {
             
                //   "newSize    Vector  ",   Vector   。    
                ensureCapacityHelper(newSize);    
            } else {
             
                //   "newSize   /   Vector  ",  newSize           null    
                for (int i = newSize ; i < elementCount ; i++) {
             
                    elementData[i] = null;    
                }    
            }    
            elementCount = newSize;    
        }    
       
        //   “Vector     ”    
        public synchronized int capacity() {
             
            return elementData.length;    
        }    
       
        //   “Vector     ”, Vector         
        public synchronized int size() {
             
            return elementCount;    
        }    
       
        //   Vector        
        public synchronized boolean isEmpty() {
             
            return elementCount == 0;    
        }    
       
        //   “Vector        Enumeration”    
        public Enumeration<E> elements() {
             
            //        Enumeration    
            return new Enumeration<E>() {
             
                int count = 0;    
       
                //              
                public boolean hasMoreElements() {
             
                    return count < elementCount;    
                }    
       
                //            
                public E nextElement() {
             
                    synchronized (Vector.this) {
             
                        if (count < elementCount) {
             
                            return (E)elementData[count++];    
                        }    
                    }    
                    throw new NoSuchElementException("Vector Enumeration");    
                }    
            };    
        }    
       
        //   Vector       (o)    
        public boolean contains(Object o) {
             
            return indexOf(o, 0) >= 0;    
        }    
       
       
        //  index          (o)。    
        //    ,         ;  ,  -1    
        public synchronized int indexOf(Object o, int index) {
             
            if (o == null) {
             
                //       null,     null  ,             
                for (int i = index ; i < elementCount ; i++)    
                if (elementData[i]==null)    
                    return i;    
            } else {
             
                //        null,        ,             
                for (int i = index ; i < elementCount ; i++)    
                if (o.equals(elementData[i]))    
                    return i;    
            }    
            return -1;    
        }    
       
        //        (o) Vector         
        public int indexOf(Object o) {
             
            return indexOf(o, 0);    
        }    
       
        //         (o)。            
        public synchronized int lastIndexOf(Object o) {
             
            return lastIndexOf(o, elementCount-1);    
        }    
       
        //         (o)。           index  ;    
        //    ,      “   ”;  ,  -1。    
        public synchronized int lastIndexOf(Object o, int index) {
             
            if (index >= elementCount)    
                throw new IndexOutOfBoundsException(index + " >= "+ elementCount);    
       
            if (o == null) {
             
                //       null,     null  ,             
                for (int i = index; i >= 0; i--)    
                if (elementData[i]==null)    
                    return i;    
            } else {
             
                //        null,        ,             
                for (int i = index; i >= 0; i--)    
                if (o.equals(elementData[i]))    
                    return i;    
            }    
            return -1;    
        }    
       
        //   Vector index     。    
        //  index  ,         
        public synchronized E elementAt(int index) {
             
            if (index >= elementCount) {
             
                throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);    
            }    
       
            return (E)elementData[index];    
        }    
       
        //   Vector       。    
        //    ,     !    
        public synchronized E firstElement() {
             
            if (elementCount == 0) {
             
                throw new NoSuchElementException();    
            }    
            return (E)elementData[0];    
        }    
       
        //   Vector        。    
        //    ,     !    
        public synchronized E lastElement() {
             
            if (elementCount == 0) {
             
                throw new NoSuchElementException();    
            }    
            return (E)elementData[elementCount - 1];    
        }    
       
        //   index       obj    
        public synchronized void setElementAt(E obj, int index) {
             
            if (index >= elementCount) {
             
                throw new ArrayIndexOutOfBoundsException(index + " >= " +    
                                     elementCount);    
            }    
            elementData[index] = obj;    
        }    
       
        //   index         
        public synchronized void removeElementAt(int index) {
             
            modCount++;    
            if (index >= elementCount) {
             
                throw new ArrayIndexOutOfBoundsException(index + " >= " +    
                                     elementCount);    
            } else if (index < 0) {
             
                throw new ArrayIndexOutOfBoundsException(index);    
            }    
       
            int j = elementCount - index - 1;    
            if (j > 0) {
             
                System.arraycopy(elementData, index + 1, elementData, index, j);    
            }    
            elementCount--;    
            elementData[elementCount] = null; /* to let gc do its work */   
        }    
       
        //  index       (obj)    
        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++;    
        }    
       
        //  “  obj”   Vector      
        public synchronized void addElement(E obj) {
             
            modCount++;    
            ensureCapacityHelper(elementCount + 1);    
            elementData[elementCount++] = obj;    
        }    
       
        //  Vector        obj。    
        //     ,  true;  ,  false。    
        public synchronized boolean removeElement(Object obj) {
             
            modCount++;    
            int i = indexOf(obj);    
            if (i >= 0) {
             
                removeElementAt(i);    
                return true;    
            }    
            return false;    
        }    
       
        //   Vector          
        public synchronized void removeAllElements() {
             
            modCount++;    
            //  Vector        null    
            for (int i = 0; i < elementCount; i++)    
                elementData[i] = null;    
       
            elementCount = 0;    
        }    
       
        //         
        public synchronized Object clone() {
             
            try {
             
                Vector<E> v = (Vector<E>) super.clone();    
                //    Vector        v     
                v.elementData = Arrays.copyOf(elementData, elementCount);    
                v.modCount = 0;    
                return v;    
            } catch (CloneNotSupportedException e) {
             
                // this shouldn't happen, since we are Cloneable    
                throw new InternalError();    
            }    
        }    
       
        //   Object      
        public synchronized Object[] toArray() {
             
            return Arrays.copyOf(elementData, elementCount);    
        }    
       
        //   Vector     。      ,    T             
        public synchronized <T> T[] toArray(T[] a) {
             
            //    a    < Vector     ;    
            //      T[]  ,     “Vector     ”,  “Vector”             
            if (a.length < elementCount)    
                return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());    
       
            //    a    >= Vector     ;    
            //   Vector           a 。    
        System.arraycopy(elementData, 0, a, 0, elementCount);    
       
            if (a.length > elementCount)    
                a[elementCount] = null;    
       
            return a;    
        }    
       
        //   index         
        public synchronized E get(int index) {
             
            if (index >= elementCount)    
                throw new ArrayIndexOutOfBoundsException(index);    
       
            return (E)elementData[index];    
        }    
       
        //   index     element。   index          
        public synchronized E set(int index, E element) {
             
            if (index >= elementCount)    
                throw new ArrayIndexOutOfBoundsException(index);    
       
            Object oldValue = elementData[index];    
            elementData[index] = element;    
            return (E)oldValue;    
        }    
       
        //  “  e”   Vector  。    
        public synchronized boolean add(E e) {
             
            modCount++;    
            ensureCapacityHelper(elementCount + 1);    
            elementData[elementCount++] = e;    
            return true;    
        }    
       
        //   Vector    o    
        public boolean remove(Object o) {
             
            return removeElement(o);    
        }    
       
        //  index      element    
        public void add(int index, E element) {
             
            insertElementAt(element, index);    
        }    
       
        //   index     ,   index          
        public synchronized E remove(int index) {
             
            modCount++;    
            if (index >= elementCount)    
                throw new ArrayIndexOutOfBoundsException(index);    
            Object oldValue = elementData[index];    
       
            int numMoved = elementCount - index - 1;    
            if (numMoved > 0)    
                System.arraycopy(elementData, index+1, elementData, index,    
                         numMoved);    
            elementData[--elementCount] = null; // Let gc do its work    
       
            return (E)oldValue;    
        }    
       
        //   Vector    
        public void clear() {
             
            removeAllElements();    
        }    
       
        //   Vector      c    
        public synchronized boolean containsAll(Collection<?> c) {
             
            return super.containsAll(c);    
        }    
       
        //    c   Vector     
        public synchronized boolean addAll(Collection<? extends E> c) {
             
            modCount++;    
            Object[] a = c.toArray();    
            int numNew = a.length;    
            ensureCapacityHelper(elementCount + numNew);    
            //    c          elementData     
            System.arraycopy(a, 0, elementData, elementCount, numNew);    
            elementCount += numNew;    
            return numNew != 0;    
        }    
       
        //     c         
        public synchronized boolean removeAll(Collection<?> c) {
             
            return super.removeAll(c);    
        }    
       
        //   “   c    ”    
        public synchronized boolean retainAll(Collection<?> c)  {
             
            return super.retainAll(c);    
        }    
       
        //  index    ,   c   Vector     
        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;    
        }    
       
        //               
        public synchronized boolean equals(Object o) {
             
            return super.equals(o);    
        }    
       
        //          
        public synchronized int hashCode() {
             
            return super.hashCode();    
        }    
       
        //      toString()    
        public synchronized String toString() {
             
            return super.toString();    
        }    
       
        //   Vector fromIndex(  ) toIndex(   )       
        public synchronized List<E> subList(int fromIndex, int toIndex) {
             
            return Collections.synchronizedList(super.subList(fromIndex, toIndex), this);    
        }    
       
        //   Vector fromIndex toIndex       
        protected synchronized void removeRange(int fromIndex, int toIndex) {
             
            modCount++;    
            int numMoved = elementCount - toIndex;    
            System.arraycopy(elementData, toIndex, elementData, fromIndex,    
                             numMoved);    
       
            // Let gc do its work    
            int newElementCount = elementCount - (toIndex-fromIndex);    
            while (elementCount != newElementCount)    
                elementData[--elementCount] = null;    
        }    
       
        // java.io.Serializable         
        private synchronized void writeObject(java.io.ObjectOutputStream s)    
            throws java.io.IOException {
             
            s.defaultWriteObject();    
        }