初探-Vector方法

1. Vector無參初始化

代碼:
	Vector<String> vector = new Vector<>();
	源代碼:
    public Vector() {
        this(10);
    }
   public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }
    
     public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }


2. Vector有參初始化

	代碼:
 	Vector<String> vector1 = new Vector<>();
	vector1.add("Hello");
	Vector<String> vector = new Vector<>(vector1);
 	源代碼:
     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);
    }
    


    
    

3. Vector添加(可添加null)

	Vector<String> vector1 = new Vector<>();
		vector1.add("Hello");
	源代碼:
    public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }


4. Vector遍歷

內部實現Iterator接口,與ArrayList一致, 
    private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        public boolean hasNext() {
            // Racy but within spec, since modifications are checked
            // within or after synchronization in next/previous
            return cursor != elementCount;
        }

        public E next() {
            synchronized (Vector.this) {
                checkForComodification();
                int i = cursor;
                if (i >= elementCount)
                    throw new NoSuchElementException();
                cursor = i + 1;
                return elementData(lastRet = i);
            }
        }

        public void remove() {
            if (lastRet == -1)
                throw new IllegalStateException();
            synchronized (Vector.this) {
                checkForComodification();
                Vector.this.remove(lastRet);
                expectedModCount = modCount;
            }
            cursor = lastRet;
            lastRet = -1;
        }

        @Override
        public void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            synchronized (Vector.this) {
                final int size = elementCount;
                int i = cursor;
                if (i >= size) {
                    return;
                }
        @SuppressWarnings("unchecked")
                final E[] elementData = (E[]) Vector.this.elementData;
                if (i >= elementData.length) {
                    throw new ConcurrentModificationException();
                }
                while (i != size && modCount == expectedModCount) {
                    action.accept(elementData[i++]);
                }
                // update once at end of iteration to reduce heap write traffic
                cursor = i;
                lastRet = i - 1;
                checkForComodification();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }


5. Vector更新

   public synchronized E set(int index, E element) {
        if (index >= elementCount) //判斷是否越界
            throw new ArrayIndexOutOfBoundsException(index);

        E oldValue = elementData(index);  // 獲取當前的元素
        elementData[index] = element;  //替換
        return oldValue;    // 返回被替換的元素
    }

  @SuppressWarnings("unchecked")
    E elementData(int index) {
        return (E) elementData[index];  // 返回下標爲index的元素
    }


6. Vector刪除

   public boolean remove(Object o) {
        return removeElement(o);
    }
  public synchronized boolean removeElement(Object obj) {
        modCount++;
        int i = indexOf(obj); // 獲取元素的下標
        if (i >= 0) {
            removeElementAt(i);
            return true;
        }
        return false;
    }
  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; // 將空出來的位置設置爲null
    }
return false; }


源代碼與ArrayList幾乎一致,但是可以看出Vector是線程安全的,效率低。ArrayList是來替代Vector.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章