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

Java中的vector类使用方法示例详解

程序员文章站 2024-03-01 13:21:22
 基本操作示例 vectorapp.java import java.util.vector; import java.lang.*; imp...

 基本操作示例

vectorapp.java

import java.util.vector; 
import java.lang.*; 
import java.util.enumeration; 
public class vectorapp 
{ 
 public static void main(string args[]) 
 { 
 vector v1 = new vector(); 
 integer integer1= new integer(1); 
 //加入为字符串对象 
 v1.addelement("one"); 
 //加入的为integer的对象 
 v1.addelement(integer1); 
 v1.addelement(integer1); 
 v1.addelement("two"); 
 v1.addelement(new integer(2)); 
 v1.addelement(integer1); 
 v1.addelement(integer1); 
 //转为字符串并打印 
 system.out.println("the vector v1 is:\n\t"+v1); 
 //向指定位置插入新对象 
 v1.insertelement("three",2); 
 v1.insertelement(new float(3.9),3); 
 system.out.println("the vector v1(used method 
 insertelementat()is:\n\t)"+v1); 
 //将指定位置的对象设置为新的对象 
 //指定位置后的对象依次往后顺延 
 v1.setelementat("four",2); 
 system.out.println("the vector v1 cused method setelmentat()is:\n\t"+v1); 
 v1.removeelement(integer1); 
 //从向量对象v1中删除对象integer1 
 //由于存在多个integer1,所以从头开始。 
 //找删除找到的第一个integer1. 
 enumeration enum = v1.elements(); 
 system.out.println("the vector v1 (used method removeelememt()is"); 
 while(enum.hasmoreelements()) 
 system.out.println(enum.nextelement()+""); 
 system.out.println(); 
 //使用枚举类(enumeration)的方法取得向量对象的每个元素。 
 system.out.println("the position of object1(top-to-botton):"+v1.indexof(integer1)); 
 system.out.println("the position of object1(tottom-to-top):"+v1.lastindexof(integer1)); 
 //按不同的方向查找对象integer1所处的位置 
 v1.setsize(4); 
 system.out.println("the new vector(resized the vector)is:"+v1); 
 //重新设置v1的大小,多余的元素被抛弃 
 } 
} 

运行结果:

e:\java01>java vectorapp 
the vector v1 is:[one,1,1,two,2,1,1] 
the vector v1(used method insetelementat()) is: 
[one,1,three,3.9,1,two,2,1,1] 
the vector v1(used method setelementat()) is: 
[one,1,four,3.9,1,two,2,1,1] 
the vector v1(useed method removeelement()) is: 
one four 3.9 1 two 2 1 1 
the position of object1(top-to-botton):3 
the position of object1(botton-to-top):7 
the new vector(resized the vector) is: 
[one,four,3.9,1] 

vertor的1倍扩容

还记得arraylist每次扩容为元数组的0.5倍不?vector在进行扩容操作时与arraylist略微不同

protected int capacityincrement;//用于指定每次扩容的容量
private void grow(int mincapacity) {
 // overflow-conscious code
 int oldcapacity = elementdata.length;
 int newcapacity = oldcapacity + ((capacityincrement > 0) ?
   capacityincrement : oldcapacity);//如不指定capacityincrement,默认扩容的容量为原数组的容量
 if (newcapacity - mincapacity < 0)
 newcapacity = mincapacity;
 if (newcapacity - max_array_size > 0)
 newcapacity = hugecapacity(mincapacity);
 elementdata = arrays.copyof(elementdata, newcapacity);
}

细心的小伙伴可以发现vector中多了一个capacityincrement变量,该变量是用于指定每次扩容的增量,如果不指定该变量,在grow中可以发现vector默认就扩容为原数组的1倍

线程安全

vertor是线程安全的!

vertor源码中另一个比较显眼的地方就是绝大部分方法都有synchronized关键字,大家都知道这个关键字是用于线程同步的,所以vector类是线程安全的!

但是即使它所有的方法都被修饰成同步,也不意味着调用它的时候永远都不需要同步手段了:

private static vector<integer> vector=new vector<integer>();
public static void main(string[] args) { 
 while(true)
 {
 for(int i=0;i<10;i++)
 {
 vector.add(i);
 }
 thread removethread=new thread(new runnable(){
 @override
 public void run()
 {
 for(int i=0;i<vector.size();i++)
 {
 vector.remove(i);
 }
 }
 });
 thread printthread=new thread(new runnable(){
 @override
 public void run()
 {
 for(int i=0;i<vector.size();i++)
 {
 system.out.println(vector.get(i));
 }
 }
 }); 
 removethread.start();
 printthread.start();
 while(thread.activecount()>20); 
 }
}

大家运行此段代码时 跑了一小段时间之后会发现有arrayindexoutofboundsexception异常,这里vector的get,remove,size方法尽管有synchronized修饰,但是在多线程环境中,如果不在方法端额外做同步措施的话,这段代码仍然是不安全的,如果一个线程删除了序号i的元素之后,另一个线程去访问这个i的话就直接回抛异常,所以保证这段代码安全还需要再run里面再添加synchronized修饰。

希望本篇vector类使用示例文章对您有所帮助