欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java中的Vector

 更新時(shí)間:2017年10月18日 16:19:51   作者:阿克西斯教成員污米飯  
Vector 可實(shí)現(xiàn)自動(dòng)增長(zhǎng)的對(duì)象數(shù)組。本文通過(guò)實(shí)例代碼給大家詳細(xì)介紹java中的vector,感興趣的朋友一起看看吧

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

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

//Vector存放的元素,初始化默認(rèn)長(zhǎng)度為10
protected Object[] elementData;
//元素個(gè)數(shù)
protected int elementCount;
//每次擴(kuò)容大小,默認(rèn)為0
protected int capacityIncrement;
//構(gòu)造函數(shù),無(wú)指定初始化大小和無(wú)擴(kuò)容大小
public Vector() {
  this(10);
}
//構(gòu)造函數(shù),指定初始化大小和無(wú)擴(kuò)容大小
public Vector(int initialCapacity) {
  this(initialCapacity, 0);
}
//構(gòu)造函數(shù),指定初始化大小和擴(kuò)容大小
public Vector(int initialCapacity, int capacityIncrement) {
  super();
  if (initialCapacity < 0)
    throw new IllegalArgumentException("Illegal Capacity: "+
                      initialCapacity);
  this.elementData = new Object[initialCapacity];
  this.capacityIncrement = capacityIncrement;
}
//構(gòu)造函數(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);
  }
//確保擴(kuò)容的最小容量
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);
}
//擴(kuò)容
private void grow(int minCapacity) {
  int oldCapacity = elementData.length;
  //當(dāng)擴(kuò)容大小為0的時(shí)候,擴(kuò)容為原來(lái)的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的最小長(zhǎng)度,當(dāng)擴(kuò)容2倍小于minCapacity時(shí),擴(kuò)容到minCapacity大小,minCapacity不能小于0
  • 最大長(zhǎng)度為2的31次方-1

設(shè)置大小

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

超過(guò)大小的被設(shè)置為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;
}

設(shè)置索引上的元素

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

擴(kuò)容

插入元素

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++;
}
  • 擴(kuò)容
  • 數(shù)組拷貝向索引后移動(dòng)
  • 刪除為向前移動(dòng)

刪除元素

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

只能刪除第一個(gè)

-我是簽名----------------------------
這里只是一個(gè)簽名

詳解

以上所述是小編給大家介紹的Java中的Vector,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論