欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Java中的Vector

程序员文章站 2024-04-01 22:40:58
vector实现了abstractlist抽象类和list接口,和arraylist一样是基于array存储的 vector 是线程安全的,在大多数方法上存在synchr...

vector实现了abstractlist抽象类和list接口,和arraylist一样是基于array存储的

vector 是线程安全的,在大多数方法上存在synchronized关键字

//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的最小长度,当扩容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++;
}
  • 扩容
  • 数组拷贝向索引后移动
  • 删除为向前移动

删除元素

public synchronized boolean removeelement(object obj) {
  modcount++;
  int i = indexof(obj);
  if (i >= 0) {
    removeelementat(i);
    return true;
  }
  return false;
}

只能删除第一个

-我是签名----------------------------
这里只是一个签名

详解

以上所述是小编给大家介绍的java中的vector,希望对大家有所帮助