溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java中Vector的作用是什么

發(fā)布時間:2021-01-16 10:12:23 來源:億速云 閱讀:196 作者:Leah 欄目:編程語言

本篇文章為大家展示了Java中Vector的作用是什么,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

Vector實現(xiàn)了AbstractList抽象類和List接口,和ArrayList一樣是基于Array存儲的

Vector 是線程安全的,在大多數(shù)方法上存在synchronized關鍵字

//Vector存放的元素,初始化默認長度為10
protected Object[] elementData;
//元素個數(shù)
protected int elementCount;
//每次擴容大小,默認為0
protected int capacityIncrement;
//構造函數(shù),無指定初始化大小和無擴容大小
public Vector() {
  this(10);
}
//構造函數(shù),指定初始化大小和無擴容大小
public Vector(int initialCapacity) {
  this(initialCapacity, 0);
}
//構造函數(shù),指定初始化大小和擴容大小
public Vector(int initialCapacity, int capacityIncrement) {
  super();
  if (initialCapacity < 0)
    throw new IllegalArgumentException("Illegal Capacity: "+
                      initialCapacity);
  this.elementData = new Object[initialCapacity];
  this.capacityIncrement = capacityIncrement;
}
//構造函數(shù),Collection集合
public Vector(Collection<? extends E> c) {
  elementData = c.toArray();
  elementCount = elementData.length;
  if (elementData.getClass() != Object[].class)
    elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
  }
//確保擴容的最小容量
public synchronized void ensureCapacity(int minCapacity) {
  if (minCapacity > 0) {
    modCount++;
    ensureCapacityHelper(minCapacity);
  }
}
private void ensureCapacityHelper(int minCapacity) {
  // overflow-conscious code
  if (minCapacity - elementData.length > 0)
    grow(minCapacity);
}
//擴容
private void grow(int minCapacity) {
  int oldCapacity = elementData.length;
  //當擴容大小為0的時候,擴容為原來的2倍
  int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                   capacityIncrement : oldCapacity);
  if (newCapacity - minCapacity < 0)
    newCapacity = minCapacity;
  if (newCapacity - MAX_ARRAY_SIZE > 0)
    newCapacity = hugeCapacity(minCapacity);
  elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
  if (minCapacity < 0) // overflow
    throw new OutOfMemoryError();
  return (minCapacity > MAX_ARRAY_SIZE) ?
    Integer.MAX_VALUE :
    MAX_ARRAY_SIZE;
}
  • ensureCapacity(int minCapacity)方法確保Vector的最小長度,當擴容2倍小于minCapacity時,擴容到minCapacity大小,minCapacity不能小于0

  • 最大長度為2的31次方-1

設置大小

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;
}

超過大小的被設置為Null

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);
  }
}
public synchronized int indexOf(Object o, int index) {
  if (o == null) {
    for (int i = index ; i < elementCount ; i++)
      if (elementData[i]==null)
        return i;
  } else {
    for (int i = index ; i < elementCount ; i++)
      if (o.equals(elementData[i]))
        return i;
  }
  return -1;
}

是否為空

public synchronized boolean isEmpty() {
  return elementCount == 0;
}

設置索引上的元素

public synchronized void setElementAt(E obj, int index) {
  if (index >= elementCount) {
    throw new ArrayIndexOutOfBoundsException(index + " >= " +
                         elementCount);
  }
  elementData[index] = obj;
}

添加元素

 public synchronized void addElement(E obj) {
  modCount++;
  ensureCapacityHelper(elementCount + 1);
  elementData[elementCount++] = 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++;
}
  • 擴容

  • 數(shù)組拷貝向索引后移動

  • 刪除為向前移動


刪除元素

public synchronized boolean removeElement(Object obj) {
  modCount++;
  int i = indexOf(obj);
  if (i >= 0) {
    removeElementAt(i);
    return true;
  }
  return false;
}

上述內容就是Java中Vector的作用是什么,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI