在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java中的vector类使用示例小结

2016-7-29 15:34| 发布者: zhangjf| 查看: 779| 评论: 0

摘要: 基本操作示例 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(); ...

基本操作示例

VectorApp.java

  1. import java.util.Vector;
  2. import java.lang.*;
  3. import java.util.Enumeration;
  4. public class VectorApp
  5. {
  6. public static void main(String args[])
  7. {
  8. Vector v1 = new Vector();
  9. Integer integer1= new Integer(1);
  10. //加入为字符串对象
  11. v1.addElement("one");
  12. //加入的为integer的对象
  13. v1.addElement(integer1);
  14. v1.addElement(integer1);
  15. v1.addElement("two");
  16. v1.addElement(new Integer(2));
  17. v1.addElement(integer1);
  18. v1.addElement(integer1);
  19. //转为字符串并打印
  20. System.out.println("The Vector v1 is:nt"+v1);
  21. //向指定位置插入新对象
  22. v1.insertElement("three",2);
  23. v1.insertElement(new Float(3.9),3);
  24. System.out.println("The Vector v1(used method
  25. insertElementAt()is:nt)"+v1);
  26. //将指定位置的对象设置为新的对象
  27. //指定位置后的对象依次往后顺延
  28. v1.setElementAt("four",2);
  29. System.out.println("The vector v1 cused method setElmentAt()is:nt"+v1);
  30. v1.removeElement(integer1);
  31. //从向量对象v1中删除对象integer1
  32. //由于存在多个integer1,所以从头开始。
  33. //找删除找到的第一个integer1.
  34. Enumeration enum = v1.elements();
  35. System.out.println("The vector v1 (used method removeElememt()is");
  36. while(enum.hasMoreElements())
  37. System.out.println(enum.nextElement()+"");
  38. System.out.println();
  39. //使用枚举类(Enumeration)的方法取得向量对象的每个元素。
  40. System.out.println("The position of Object1(top-to-botton):"+v1.indexOf(integer1));
  41. System.out.println("The position of Object1(tottom-to-top):"+v1.lastIndexOf(integer1));
  42. //按不同的方向查找对象integer1所处的位置
  43. v1.setSize(4);
  44. System.out.println("The new Vector(resized the vector)is:"+v1);
  45. //重新设置v1的大小,多余的元素被抛弃
  46. }
  47. }
复制代码

运行结果:

  1. E:java01>java VectorApp
  2. The vector v1 is:[one,1,1,two,2,1,1]
  3. The vector v1(used method insetElementAt()) is:
  4. [one,1,three,3.9,1,two,2,1,1]
  5. The vector v1(used method setElementAt()) is:
  6. [one,1,four,3.9,1,two,2,1,1]
  7. The vector v1(useed method removeElement()) is:
  8. one four 3.9 1 two 2 1 1
  9. The position of object1(top-to-botton):3
  10. The position of object1(botton-to-top):7
  11. The new Vector(resized the vector) is:
  12. [one,four,3.9,1]
复制代码


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

  1. protected int capacityIncrement;//用于指定每次扩容的容量
  2. private void grow(int minCapacity) {
  3. // overflow-conscious code
  4. int oldCapacity = elementData.length;
  5. int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
  6. capacityIncrement : oldCapacity);//如不指定capacityIncrement,默认扩容的容量为原数组的容量
  7. if (newCapacity - minCapacity < 0)
  8. newCapacity = minCapacity;
  9. if (newCapacity - MAX_ARRAY_SIZE > 0)
  10. newCapacity = hugeCapacity(minCapacity);
  11. elementData = Arrays.copyOf(elementData, newCapacity);
  12. }
复制代码

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

线程安全
Vertor是线程安全的!
Vertor源码中另一个比较显眼的地方就是绝大部分方法都有synchronized关键字,大家都知道这个关键字是用于线程同步的,所以Vector类是线程安全的!
但是即使它所有的方法都被修饰成同步,也不意味着调用它的时候永远都不需要同步手段了:

  1. private static Vector<Integer> vector=new Vector<Integer>();
  2. public static void main(String[] args) {
  3. while(true)
  4. {
  5. for(int i=0;i<10;i++)
  6. {
  7. vector.add(i);
  8. }
  9. Thread removeThread=new Thread(new Runnable(){
  10. @Override
  11. public void run()
  12. {
  13. for(int i=0;i<vector.size();i++)
  14. {
  15. vector.remove(i);
  16. }
  17. }
  18. });
  19. Thread printThread=new Thread(new Runnable(){
  20. @Override
  21. public void run()
  22. {
  23. for(int i=0;i<vector.size();i++)
  24. {
  25. System.out.println(vector.get(i));
  26. }
  27. }
  28. });
  29. removeThread.start();
  30. printThread.start();
  31. while(Thread.activeCount()>20);
  32. }
  33. }
复制代码

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

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-4 02:23

Copyright 2015-2025 djqfx

返回顶部