>  기사  >  Java  >  Java의 Vector에 대한 자세한 소개

Java의 Vector에 대한 자세한 소개

黄舟
黄舟원래의
2017-10-19 09:31:311779검색

Vector는 자동으로 증가하는 객체 배열을 구현할 수 있습니다. 이 글에서는 Java의 벡터에 대해 자세히 설명합니다. 관심 있는 친구들은 함께 살펴보세요.

Vector는 ArrayList와 마찬가지로 Array 저장소를 기반으로 합니다. Vector는 스레드로부터 안전합니다.

//Vector存放的元素,初始化默认长度为10
protected Object[] elementData;
//元素个数
protected int elementCount;
//每次扩容大小,默认为0
protected int capacityIncrement;
//构造函数,无指定初始化大小和无扩容大小
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;
}
//构造函数,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의 최소 길이가 minCapacity보다 2배 작을 때 minCapacity로 확장됩니다. minCapacity는 0
  • max보다 작을 수 없습니다. 길이는 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;
}

Expansion

요소 삽입

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

    확장
  • 배열 복사 이동 색인 뒤로
  • 앞으로 삭제

  • Delete elements

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

첫 번째 항목만 삭제할 수 있습니다


-제가 서명입니다---- -------- ---

이것은 단지 서명입니다


자세한 설명

위 내용은 Java의 Vector에 대한 자세한 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.