首页 > Java > java教程 > 正文

Java中的vector类使用示例小结

黄舟
发布: 2017-01-20 11:04:56
原创
1845人浏览过

vector与arraylist的实现基本相似,同样是基于动态数组,同样是需要扩容,下面举了三个简短的例子来帮助大家理解vertor:

基本操作示例

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略微不同

立即学习Java免费学习笔记(深入)”;

86CMS企业网站系统中英繁三语版1.2
86CMS企业网站系统中英繁三语版1.2

86CMS企业网站系统为智能ASP网站管理程序,适合中小企业自建网站、二次开发使用。本程序具有体积小巧、程序文件结构严谨、界面清爽简单、功能强大、非专业人士使用入门快、中小企业使用投资小等实用特点。本版本为中英繁版本。86CMS企业网站系统中英繁三语版 v1.2 更新1.修正英文版的flash幻灯调用出错问题。2.修正英文版导航菜单设置出错问题。3.增加信息是否显示在中英各版的功能。4.调整首页视

86CMS企业网站系统中英繁三语版1.2 1
查看详情 86CMS企业网站系统中英繁三语版1.2
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修饰

以上就是Java中的vector类使用示例小结的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关标签:
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号