Maison  >  Article  >  Java  >  Introduction détaillée à Vector en Java

Introduction détaillée à Vector en Java

黄舟
黄舟original
2017-10-19 09:31:311780parcourir

Vector peut implémenter un tableau d'objets à croissance automatique. Cet article présente en détail Vector en Java à travers un exemple de code. Les amis intéressés devraient y jeter un œil ensemble

Vector implémente la classe abstraite AbstractList et l'interface List, et comme ArrayList, il est basé sur le stockage Array

Vector est thread-safe et le mot-clé synchronisé existe sur la plupart des méthodes. Longueur, lorsque l'expansion est inférieure à minCapacity de 2 fois, étendez-la à la taille de minCapacity ne peut pas être inférieure à 0

.

La longueur maximale est de 2 à la puissance 31 -1
//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;
}
  • Taille définie

  • Dépassement la taille est définie sur Null


Qu'elle soit vide
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;
}


définir l'élément sur l'index
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;
}


ajouter un élément
public synchronized boolean isEmpty() {
  return elementCount == 0;
}


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

Insérer un élément


 public synchronized void addElement(E obj) {
  modCount++;
  ensureCapacityHelper(elementCount + 1);
  elementData[elementCount++] = obj;
}

Développer


La copie du tableau est déplacée à la fin de l'index
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++;
}
  • est supprimé et avancé

  • Supprimer l'élément

  • Seulement le premier

-Je suis une signature--- --------------------- -----

Ceci est juste une signature

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

Explication détaillée

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn