①对Vector有个整体认识

Vector是向量类,继承于AbstractList,实现了List,RandomAccess,Clonable这些接口。

Vector继承于AbstractList,实现了List,它是一个队列,支持相关的添加、删除、修改、遍历等功能

Vector实现了RandomAccess接口,即提供了随机访问功能。RandomAccess是Java中来被List实现,为List提供快速访问功能的,在Vecor中,我们可以通过元素的序号快速获取元素对象,这就是快速随机访问。

Vector实现了Cloneable接口,即clone()函数

②Vector的构造函数

  1. //默认构造函数
  2. Vector()
  3.  
  4. //capacity是Vector()的默认容量大小,当由于增加数据导致容量增加时,每次容量会增加一倍。
  5. Vector(int capacity)
  6.  
  7. // capacity是Vector()的默认容量大小,capacityIncrement是每次Vector容量增加时的增量值。
  8. Vector(int capacity,int capacityIncrement)
  9.  
  10. //创建一个包含包含collection的Vector
  11. Vector(Collection<? Extends E> collection)

③Vector的API

  1. synchronized boolean add(E object)
  2. void add(int location, E object)
  3. synchronized boolean addAll(Collection<? extends E> collection)
  4. synchronized boolean addAll(int location, Collection<? extends E> collection)
  5. synchronized void addElement(E object)
  6. synchronized int capacity()
  7. void clear()
  8. synchronized Object clone()
  9. boolean contains(Object object)
  10. synchronized boolean containsAll(Collection<?> collection)
  11. synchronized void copyInto(Object[] elements)
  12. synchronized E elementAt(int location)
  13. Enumeration<E> elements()
  14. synchronized void ensureCapacity(int minimumCapacity)
  15. synchronized boolean equals(Object object)
  16. synchronized E firstElement()
  17. E get(int location)
  18. synchronized int hashCode()
  19. synchronized int indexOf(Object object, int location)
  20. int indexOf(Object object)
  21. synchronized void insertElementAt(E object, int location)
  22. synchronized boolean isEmpty()
  23. synchronized E lastElement()
  24. synchronized int lastIndexOf(Object object, int location)
  25. synchronized int lastIndexOf(Object object)
  26. synchronized E remove(int location)
  27. boolean remove(Object object)
  28. synchronized boolean removeAll(Collection<?> collection)
  29. synchronized void removeAllElements()
  30. synchronized boolean removeElement(Object object)
  31. synchronized void removeElementAt(int location)
  32. synchronized boolean retainAll(Collection<?> collection)
  33. synchronized E set(int location, E object)
  34. synchronized void setElementAt(E object, int location)
  35. synchronized void setSize(int length)
  36. synchronized int size()
  37. synchronized List<E> subList(int start, int end)
  38. synchronized <T> T[] toArray(T[] contents)
  39. synchronized Object[] toArray()
  40. synchronized String toString()
  41. synchronized void trimToSize()

④Vector的继承关系

  1. java.lang.Object
  2. java.util.AbstractCollection<E>
  3. java.util.AbstractList<E>
  4. java.util.Vector<E>
  5.  
  6. public class Vector<E>
  7. extends AbstractList<E>
  8. implements List<E>, RandomAccess, Cloneable, java.io.Serializable {}

⑤Vector与Collection关系

⑥Vector的数据结构

Vector的数据结构包含了3个成员变量:elementData,elementCount,capacityIncrement

elementData是Object[]类型的数组,它保存了添加到Vector中的元素。elementData是个动态数组,如果初始化Vector时,没有指定动态数组的大小,则使用默认大小10。随着Vector中元素的增加,Vector的容量也会动态增长,capacityIncrement是与容量增长相关的增长指数,具体的增长方式请参考源码分析中的ensureCapacity()函数。

elementCount是动态数组的实际大小

capacityIncrement是动态数组的增长系数。如果在创建Vector时,指定了capacityIncrement的大小,则每次Vector中动态数组容量增加时,增加的大小都是capacityIncrement.

⑦ 源码及解析

  1. package java.util;
  2.  
  3. public class Vector<E>
  4. extends AbstractList<E>
  5. implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  6. {
  7.  
  8. // 保存Vector中数据的数组
  9. protected Object[] elementData;
  10.  
  11. // 实际数据的数量
  12. protected int elementCount;
  13.  
  14. // 容量增长系数
  15. protected int capacityIncrement;
  16.  
  17. // Vector的序列版本号
  18. private static final long serialVersionUID = -2767605614048989439L;
  19.  
  20. // Vector构造函数。默认容量是10。
  21. public Vector() {
  22. this(10);
  23. }
  24.  
  25. // 指定Vector容量大小的构造函数
  26. public Vector(int initialCapacity) {
  27. this(initialCapacity, 0);
  28. }
  29.  
  30. // 指定Vector"容量大小"和"增长系数"的构造函数
  31. public Vector(int initialCapacity, int capacityIncrement) {
  32. super();
  33. if (initialCapacity < 0)
  34. throw new IllegalArgumentException("Illegal Capacity: "+
  35. initialCapacity);
  36. // 新建一个数组,数组容量是initialCapacity
  37. this.elementData = new Object[initialCapacity];
  38. // 设置容量增长系数
  39. this.capacityIncrement = capacityIncrement;
  40. }
  41.  
  42. // 指定集合的Vector构造函数。
  43. public Vector(Collection<? extends E> c) {
  44. // 获取“集合(c)”的数组,并将其赋值给elementData
  45. elementData = c.toArray();
  46. // 设置数组长度
  47. elementCount = elementData.length;
  48. // c.toArray might (incorrectly) not return Object[] (see 6260652)
  49. if (elementData.getClass() != Object[].class)
  50. elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
  51. }
  52.  
  53. // 将数组Vector的全部元素都拷贝到数组anArray中
  54. public synchronized void copyInto(Object[] anArray) {
  55. System.arraycopy(elementData, 0, anArray, 0, elementCount);
  56. }
  57.  
  58. // 将当前容量值设为 =实际元素个数
  59. public synchronized void trimToSize() {
  60. modCount++;
  61. int oldCapacity = elementData.length;
  62. if (elementCount < oldCapacity) {
  63. elementData = Arrays.copyOf(elementData, elementCount);
  64. }
  65. }
  66.  
  67. // 确认“Vector容量”的帮助函数
  68. private void ensureCapacityHelper(int minCapacity) {
  69. int oldCapacity = elementData.length;
  70. // 当Vector的容量不足以容纳当前的全部元素,增加容量大小。
  71. // 若 容量增量系数>0(即capacityIncrement>0),则将容量增大当capacityIncrement
  72. // 否则,将容量增大一倍。
  73. if (minCapacity > oldCapacity) {
  74. Object[] oldData = elementData;
  75. int newCapacity = (capacityIncrement > 0) ?
  76. (oldCapacity + capacityIncrement) : (oldCapacity * 2);
  77. if (newCapacity < minCapacity) {
  78. newCapacity = minCapacity;
  79. }
  80. elementData = Arrays.copyOf(elementData, newCapacity);
  81. }
  82. }
  83.  
  84. // 确定Vector的容量。
  85. public synchronized void ensureCapacity(int minCapacity) {
  86. // 将Vector的改变统计数+1
  87. modCount++;
  88. ensureCapacityHelper(minCapacity);
  89. }
  90.  
  91. // 设置容量值为 newSize
  92. public synchronized void setSize(int newSize) {
  93. modCount++;
  94. if (newSize > elementCount) {
  95. // 若 "newSize 大于 Vector容量",则调整Vector的大小。
  96. ensureCapacityHelper(newSize);
  97. } else {
  98. // 若 "newSize 小于/等于 Vector容量",则将newSize位置开始的元素都设置为null
  99. for (int i = newSize ; i < elementCount ; i++) {
  100. elementData[i] = null;
  101. }
  102. }
  103. elementCount = newSize;
  104. }
  105.  
  106. // 返回“Vector的总的容量”
  107. public synchronized int capacity() {
  108. return elementData.length;
  109. }
  110.  
  111. // 返回“Vector的实际大小”,即Vector中元素个数
  112. public synchronized int size() {
  113. return elementCount;
  114. }
  115.  
  116. // 判断Vector是否为空
  117. public synchronized boolean isEmpty() {
  118. return elementCount == 0;
  119. }
  120.  
  121. // 返回“Vector中全部元素对应的Enumeration”
  122. public Enumeration<E> elements() {
  123. // 通过匿名类实现Enumeration
  124. return new Enumeration<E>() {
  125. int count = 0;
  126.  
  127. // 是否存在下一个元素
  128. public boolean hasMoreElements() {
  129. return count < elementCount;
  130. }
  131.  
  132. // 获取下一个元素
  133. public E nextElement() {
  134. synchronized (Vector.this) {
  135. if (count < elementCount) {
  136. return (E)elementData[count++];
  137. }
  138. }
  139. throw new NoSuchElementException("Vector Enumeration");
  140. }
  141. };
  142. }
  143.  
  144. // 返回Vector中是否包含对象(o)
  145. public boolean contains(Object o) {
  146. return indexOf(o, 0) >= 0;
  147. }
  148.  
  149. // 从index位置开始向后查找元素(o)。
  150. // 若找到,则返回元素的索引值;否则,返回-1
  151. public synchronized int indexOf(Object o, int index) {
  152. if (o == null) {
  153. // 若查找元素为null,则正向找出null元素,并返回它对应的序号
  154. for (int i = index ; i < elementCount ; i++)
  155. if (elementData[i]==null)
  156. return i;
  157. } else {
  158. // 若查找元素不为null,则正向找出该元素,并返回它对应的序号
  159. for (int i = index ; i < elementCount ; i++)
  160. if (o.equals(elementData[i]))
  161. return i;
  162. }
  163. return -1;
  164. }
  165.  
  166. // 查找并返回元素(o)在Vector中的索引值
  167. public int indexOf(Object o) {
  168. return indexOf(o, 0);
  169. }
  170.  
  171. // 从后向前查找元素(o)。并返回元素的索引
  172. public synchronized int lastIndexOf(Object o) {
  173. return lastIndexOf(o, elementCount-1);
  174. }
  175.  
  176. // 从后向前查找元素(o)。开始位置是从前向后的第index个数;
  177. // 若找到,则返回元素的“索引值”;否则,返回-1。
  178. public synchronized int lastIndexOf(Object o, int index) {
  179. if (index >= elementCount)
  180. throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
  181.  
  182. if (o == null) {
  183. // 若查找元素为null,则反向找出null元素,并返回它对应的序号
  184. for (int i = index; i >= 0; i--)
  185. if (elementData[i]==null)
  186. return i;
  187. } else {
  188. // 若查找元素不为null,则反向找出该元素,并返回它对应的序号
  189. for (int i = index; i >= 0; i--)
  190. if (o.equals(elementData[i]))
  191. return i;
  192. }
  193. return -1;
  194. }
  195.  
  196. // 返回Vector中index位置的元素。
  197. // 若index月结,则抛出异常
  198. public synchronized E elementAt(int index) {
  199. if (index >= elementCount) {
  200. throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
  201. }
  202.  
  203. return (E)elementData[index];
  204. }
  205.  
  206. // 获取Vector中的第一个元素。
  207. // 若失败,则抛出异常!
  208. public synchronized E firstElement() {
  209. if (elementCount == 0) {
  210. throw new NoSuchElementException();
  211. }
  212. return (E)elementData[0];
  213. }
  214.  
  215. // 获取Vector中的最后一个元素。
  216. // 若失败,则抛出异常!
  217. public synchronized E lastElement() {
  218. if (elementCount == 0) {
  219. throw new NoSuchElementException();
  220. }
  221. return (E)elementData[elementCount - 1];
  222. }
  223.  
  224. // 设置index位置的元素值为obj
  225. public synchronized void setElementAt(E obj, int index) {
  226. if (index >= elementCount) {
  227. throw new ArrayIndexOutOfBoundsException(index + " >= " +
  228. elementCount);
  229. }
  230. elementData[index] = obj;
  231. }
  232.  
  233. // 删除index位置的元素
  234. public synchronized void removeElementAt(int index) {
  235. modCount++;
  236. if (index >= elementCount) {
  237. throw new ArrayIndexOutOfBoundsException(index + " >= " +
  238. elementCount);
  239. } else if (index < 0) {
  240. throw new ArrayIndexOutOfBoundsException(index);
  241. }
  242.  
  243. int j = elementCount - index - 1;
  244. if (j > 0) {
  245. System.arraycopy(elementData, index + 1, elementData, index, j);
  246. }
  247. elementCount--;
  248. elementData[elementCount] = null; /* to let gc do its work */
  249. }
  250.  
  251. // 在index位置处插入元素(obj)
  252. public synchronized void insertElementAt(E obj, int index) {
  253. modCount++;
  254. if (index > elementCount) {
  255. throw new ArrayIndexOutOfBoundsException(index
  256. + " > " + elementCount);
  257. }
  258. ensureCapacityHelper(elementCount + 1);
  259. System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
  260. elementData[index] = obj;
  261. elementCount++;
  262. }
  263.  
  264. // 将“元素obj”添加到Vector末尾
  265. public synchronized void addElement(E obj) {
  266. modCount++;
  267. ensureCapacityHelper(elementCount + 1);
  268. elementData[elementCount++] = obj;
  269. }
  270.  
  271. // 在Vector中查找并删除元素obj。
  272. // 成功的话,返回true;否则,返回false。
  273. public synchronized boolean removeElement(Object obj) {
  274. modCount++;
  275. int i = indexOf(obj);
  276. if (i >= 0) {
  277. removeElementAt(i);
  278. return true;
  279. }
  280. return false;
  281. }
  282.  
  283. // 删除Vector中的全部元素
  284. public synchronized void removeAllElements() {
  285. modCount++;
  286. // 将Vector中的全部元素设为null
  287. for (int i = 0; i < elementCount; i++)
  288. elementData[i] = null;
  289.  
  290. elementCount = 0;
  291. }
  292.  
  293. // 克隆函数
  294. public synchronized Object clone() {
  295. try {
  296. Vector<E> v = (Vector<E>) super.clone();
  297. // 将当前Vector的全部元素拷贝到v中
  298. v.elementData = Arrays.copyOf(elementData, elementCount);
  299. v.modCount = 0;
  300. return v;
  301. } catch (CloneNotSupportedException e) {
  302. // this shouldn't happen, since we are Cloneable
  303. throw new InternalError();
  304. }
  305. }
  306.  
  307. // 返回Object数组
  308. public synchronized Object[] toArray() {
  309. return Arrays.copyOf(elementData, elementCount);
  310. }
  311.  
  312. // 返回Vector的模板数组。所谓模板数组,即可以将T设为任意的数据类型
  313. public synchronized <T> T[] toArray(T[] a) {
  314. // 若数组a的大小 < Vector的元素个数;
  315. // 则新建一个T[]数组,数组大小是“Vector的元素个数”,并将“Vector”全部拷贝到新数组中
  316. if (a.length < elementCount)
  317. return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
  318.  
  319. // 若数组a的大小 >= Vector的元素个数;
  320. // 则将Vector的全部元素都拷贝到数组a中。
  321. System.arraycopy(elementData, 0, a, 0, elementCount);
  322.  
  323. if (a.length > elementCount)
  324. a[elementCount] = null;
  325.  
  326. return a;
  327. }
  328.  
  329. // 获取index位置的元素
  330. public synchronized E get(int index) {
  331. if (index >= elementCount)
  332. throw new ArrayIndexOutOfBoundsException(index);
  333.  
  334. return (E)elementData[index];
  335. }
  336.  
  337. // 设置index位置的值为element。并返回index位置的原始值
  338. public synchronized E set(int index, E element) {
  339. if (index >= elementCount)
  340. throw new ArrayIndexOutOfBoundsException(index);
  341.  
  342. Object oldValue = elementData[index];
  343. elementData[index] = element;
  344. return (E)oldValue;
  345. }
  346.  
  347. // 将“元素e”添加到Vector最后。
  348. public synchronized boolean add(E e) {
  349. modCount++;
  350. ensureCapacityHelper(elementCount + 1);
  351. elementData[elementCount++] = e;
  352. return true;
  353. }
  354.  
  355. // 删除Vector中的元素o
  356. public boolean remove(Object o) {
  357. return removeElement(o);
  358. }
  359.  
  360. // 在index位置添加元素element
  361. public void add(int index, E element) {
  362. insertElementAt(element, index);
  363. }
  364.  
  365. // 删除index位置的元素,并返回index位置的原始值
  366. public synchronized E remove(int index) {
  367. modCount++;
  368. if (index >= elementCount)
  369. throw new ArrayIndexOutOfBoundsException(index);
  370. Object oldValue = elementData[index];
  371.  
  372. int numMoved = elementCount - index - 1;
  373. if (numMoved > 0)
  374. System.arraycopy(elementData, index+1, elementData, index,
  375. numMoved);
  376. elementData[--elementCount] = null; // Let gc do its work
  377.  
  378. return (E)oldValue;
  379. }
  380.  
  381. // 清空Vector
  382. public void clear() {
  383. removeAllElements();
  384. }
  385.  
  386. // 返回Vector是否包含集合c
  387. public synchronized boolean containsAll(Collection<?> c) {
  388. return super.containsAll(c);
  389. }
  390.  
  391. // 将集合c添加到Vector中
  392. public synchronized boolean addAll(Collection<? extends E> c) {
  393. modCount++;
  394. Object[] a = c.toArray();
  395. int numNew = a.length;
  396. ensureCapacityHelper(elementCount + numNew);
  397. // 将集合c的全部元素拷贝到数组elementData中
  398. System.arraycopy(a, 0, elementData, elementCount, numNew);
  399. elementCount += numNew;
  400. return numNew != 0;
  401. }
  402.  
  403. // 删除集合c的全部元素
  404. public synchronized boolean removeAll(Collection<?> c) {
  405. return super.removeAll(c);
  406. }
  407.  
  408. // 删除“非集合c中的元素”
  409. public synchronized boolean retainAll(Collection<?> c) {
  410. return super.retainAll(c);
  411. }
  412.  
  413. // 从index位置开始,将集合c添加到Vector中
  414. public synchronized boolean addAll(int index, Collection<? extends E> c) {
  415. modCount++;
  416. if (index < 0 || index > elementCount)
  417. throw new ArrayIndexOutOfBoundsException(index);
  418.  
  419. Object[] a = c.toArray();
  420. int numNew = a.length;
  421. ensureCapacityHelper(elementCount + numNew);
  422.  
  423. int numMoved = elementCount - index;
  424. if (numMoved > 0)
  425. System.arraycopy(elementData, index, elementData, index + numNew, numMoved);
  426.  
  427. System.arraycopy(a, 0, elementData, index, numNew);
  428. elementCount += numNew;
  429. return numNew != 0;
  430. }
  431.  
  432. // 返回两个对象是否相等
  433. public synchronized boolean equals(Object o) {
  434. return super.equals(o);
  435. }
  436.  
  437. // 计算哈希值
  438. public synchronized int hashCode() {
  439. return super.hashCode();
  440. }
  441.  
  442. // 调用父类的toString()
  443. public synchronized String toString() {
  444. return super.toString();
  445. }
  446.  
  447. // 获取Vector中fromIndex(包括)到toIndex(不包括)的子集
  448. public synchronized List<E> subList(int fromIndex, int toIndex) {
  449. return Collections.synchronizedList(super.subList(fromIndex, toIndex), this);
  450. }
  451.  
  452. // 删除Vector中fromIndex到toIndex的元素
  453. protected synchronized void removeRange(int fromIndex, int toIndex) {
  454. modCount++;
  455. int numMoved = elementCount - toIndex;
  456. System.arraycopy(elementData, toIndex, elementData, fromIndex,
  457. numMoved);
  458.  
  459. // Let gc do its work
  460. int newElementCount = elementCount - (toIndex-fromIndex);
  461. while (elementCount != newElementCount)
  462. elementData[--elementCount] = null;
  463. }
  464.  
  465. // java.io.Serializable的写入函数
  466. private synchronized void writeObject(java.io.ObjectOutputStream s)
  467. throws java.io.IOException {
  468. s.defaultWriteObject();
  469. }
  470. }

Vector实际上是通过一个数组去保存数据的。当我们构造Vecotr时;若使用默认构造函数,则Vector的默认容量大小是10
当Vector容量不足以容纳全部元素时,Vector的容量会增加。若容量增加系数 >0,则将容量的值增加“容量增加系数”;否则,将容量大小增加一倍。
Vector的克隆函数,即是将全部元素克隆到一个数组中。

⑧Vector遍历方式

通过迭代器遍历

  1. Integer value=null;
  2. Int size=vec.size();
  3. For(int i=0;i<size;i++){
  4. Value=(Integer)vec.get(i);
  5. }

随机访问,通过索引值去遍历

  1. Integer value=null;
  2. Int size =vec.size();
  3. For(int i=0;i<size;i++){
  4. Value=(Integer)vec.get(i);
  5. }

另一种for循环

  1. Integer value=null;
  2. For(Integer integ:vec){
  3. Value=integ;
  4. }

Enumeration遍历

  1. Integer enu=vec.elements();
  2. While (enu.hasMoreElements()){
  3. Value=(Integer)enu.nextElement();
  4. }

⑨ 测试这些遍历方式效率的代码如下

  1. import java.util.*;
  2.  
  3. /*
  4. * @desc Vector遍历方式和效率的测试程序。
  5. *
  6. * @author skywang
  7. */
  8. public class VectorRandomAccessTest {
  9.  
  10. public static void main(String[] args) {
  11. Vector vec= new Vector();
  12. for (int i=0; i<100000; i++)
  13. vec.add(i);
  14. iteratorThroughRandomAccess(vec) ;
  15. iteratorThroughIterator(vec) ;
  16. iteratorThroughFor2(vec) ;
  17. iteratorThroughEnumeration(vec) ;
  18.  
  19. }
  20.  
  21. private static void isRandomAccessSupported(List list) {
  22. if (list instanceof RandomAccess) {
  23. System.out.println("RandomAccess implemented!");
  24. } else {
  25. System.out.println("RandomAccess not implemented!");
  26. }
  27.  
  28. }
  29.  
  30. public static void iteratorThroughRandomAccess(List list) {
  31.  
  32. long startTime;
  33. long endTime;
  34. startTime = System.currentTimeMillis();
  35. for (int i=0; i<list.size(); i++) {
  36. list.get(i);
  37. }
  38. endTime = System.currentTimeMillis();
  39. long interval = endTime - startTime;
  40. System.out.println("iteratorThroughRandomAccess:" + interval+" ms");
  41. }
  42.  
  43. public static void iteratorThroughIterator(List list) {
  44.  
  45. long startTime;
  46. long endTime;
  47. startTime = System.currentTimeMillis();
  48. for(Iterator iter = list.iterator(); iter.hasNext(); ) {
  49. iter.next();
  50. }
  51. endTime = System.currentTimeMillis();
  52. long interval = endTime - startTime;
  53. System.out.println("iteratorThroughIterator:" + interval+" ms");
  54. }
  55.  
  56. public static void iteratorThroughFor2(List list) {
  57.  
  58. long startTime;
  59. long endTime;
  60. startTime = System.currentTimeMillis();
  61. for(Object obj:list)
  62. ;
  63. endTime = System.currentTimeMillis();
  64. long interval = endTime - startTime;
  65. System.out.println("iteratorThroughFor2:" + interval+" ms");
  66. }
  67.  
  68. public static void iteratorThroughEnumeration(Vector vec) {
  69.  
  70. long startTime;
  71. long endTime;
  72. startTime = System.currentTimeMillis();
  73. for(Enumeration enu = vec.elements(); enu.hasMoreElements(); ) {
  74. enu.nextElement();
  75. }
  76. endTime = System.currentTimeMillis();
  77. long interval = endTime - startTime;
  78. System.out.println("iteratorThroughEnumeration:" + interval+" ms");
  79. }
  80. }

⑩Vector示例

  1. import java.util.Vector;
  2. import java.util.List;
  3. import java.util.Iterator;
  4. import java.util.Enumeration;
  5.  
  6. /**
  7. * @desc Vector测试函数:遍历Vector和常用API
  8. *
  9. * @author skywang
  10. */
  11. public class VectorTest {
  12. public static void main(String[] args) {
  13. // 新建Vector
  14. Vector vec = new Vector();
  15.  
  16. // 添加元素
  17. vec.add("1");
  18. vec.add("2");
  19. vec.add("3");
  20. vec.add("4");
  21. vec.add("5");
  22.  
  23. // 设置第一个元素为100
  24. vec.set(0, "100");
  25. // 将“500”插入到第3个位置
  26. vec.add(2, "300");
  27. System.out.println("vec:"+vec);
  28.  
  29. // (顺序查找)获取100的索引
  30. System.out.println("vec.indexOf(100):"+vec.indexOf("100"));
  31. // (倒序查找)获取100的索引
  32. System.out.println("vec.lastIndexOf(100):"+vec.lastIndexOf("100"));
  33. // 获取第一个元素
  34. System.out.println("vec.firstElement():"+vec.firstElement());
  35. // 获取第3个元素
  36. System.out.println("vec.elementAt(2):"+vec.elementAt(2));
  37. // 获取最后一个元素
  38. System.out.println("vec.lastElement():"+vec.lastElement());
  39.  
  40. // 获取Vector的大小
  41. System.out.println("size:"+vec.size());
  42. // 获取Vector的总的容量
  43. System.out.println("capacity:"+vec.capacity());
  44.  
  45. // 获取vector的“第2”到“第4”个元素
  46. System.out.println("vec 2 to 4:"+vec.subList(1, 4));
  47.  
  48. // 通过Enumeration遍历Vector
  49. Enumeration enu = vec.elements();
  50. while(enu.hasMoreElements())
  51. System.out.println("nextElement():"+enu.nextElement());
  52.  
  53. Vector retainVec = new Vector();
  54. retainVec.add("100");
  55. retainVec.add("300");
  56. // 获取“vec”中包含在“retainVec中的元素”的集合
  57. System.out.println("vec.retain():"+vec.retainAll(retainVec));
  58. System.out.println("vec:"+vec);
  59.  
  60. // 获取vec对应的String数组
  61. String[] arr = (String[]) vec.toArray(new String[0]);
  62. for (String str:arr)
  63. System.out.println("str:"+str);
  64.  
  65. // 清空Vector。clear()和removeAllElements()一样!
  66. vec.clear();
  67. // vec.removeAllElements();
  68.  
  69. // 判断Vector是否为空
  70. System.out.println("vec.isEmpty():"+vec.isEmpty());
  71. }
  72. }

out

  1. vec:[100, 2, 300, 3, 4, 5]
  2. vec.indexOf(100):0
  3. vec.lastIndexOf(100):0
  4. vec.firstElement():100
  5. vec.elementAt(2):300
  6. vec.lastElement():5
  7. size:6
  8. capacity:10
  9. vec 2 to 4:[2, 300, 3]
  10. nextElement():100
  11. nextElement():2
  12. nextElement():300
  13. nextElement():3
  14. nextElement():4
  15. nextElement():5
  16. vec.retain():true
  17. vec:[100, 300]
  18. str:100
  19. str:300
  20. vec.isEmpty():true

Java Vertor详细介绍和使用示例的更多相关文章

  1. Java linkedList详细介绍及使用示例

    ①LinkedList简单介绍 是一个继承于AbstractSequentialList的双向链表.它可以被当成堆栈.队列或双端队列进行操作. 实现了List接口,能对它进行队列操作. 实现了Dequ ...

  2. Java ArrayList详细介绍和使用示例

    ①对ArrayList的整体认识 ArrayList是一个数组队列,相当于动态数组.与Java中的数组相比,它的容量能动态增长.它继承了AbstractList,实现了List,RandomAcces ...

  3. Java TreeMap详细介绍和使用示例

    ①对TreeMap有个整体认识 TreeMap是一个有序的key-value集合,它是通过红黑树实现的. TreeMap继承于AbstractMap,所以它是一个Map,即key-value集合. T ...

  4. Java Hashtable详细介绍和使用示例

    ①对Hashtable有个整体认识 和HashMap一样,Hashtable 也是一个散列表,它存储的内容是键值对(key-value)映射.Hashtable 继承于Dictionary,实现了Ma ...

  5. Java HashMap详细介绍和使用示例

    ①对HashMap的整体认识 HashMap是一个散列表,它存储的内容是键值对(key-value)映射. HashMap继承于AbstractMap,实现了Map.Cloneable.java.io ...

  6. java agent 详细介绍 -javaagent参数

    java agent 详细介绍 简介 java agent是java命令的一个参数.参数 javaagent 可以用于指定一个 jar 包,并且对该 java 包有2个要求: 这个 jar 包的MAN ...

  7. JAVA HashMap详细介绍和示例

    http://www.jb51.net/article/42769.htm 我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.   第1部分 HashMa ...

  8. Java反射详细介绍

    反射 目录介绍 1.反射概述 1.1 反射概述 1.2 获取class文件对象的三种方式 1.3 反射常用的方法介绍 1.4 反射的定义 1.5 反射的组成 1.6 反射的作用有哪些 2.反射的相关使 ...

  9. java jodd框架介绍及使用示例

    Jodd是一个普通开源Java包.你可以把Jodd想象成Java的"瑞士军刀",不仅小,锋利而且包含许多便利的功能.Jodd 提供的功能有:  提供操作Java bean,  可以 ...

随机推荐

  1. python3----scrapy(笔记)

    import scrapy import sys # import io # sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb ...

  2. connect() failed (111: Connection refused) while connecting to upstream, cli

    php-fpm没有运行 执行如下命令查看是否启动了php-fpm,如果没有则启动你的php-fpm即可 netstat -ant | grep 9000 没有运行为空,有运行显示 tcp 0 0 12 ...

  3. ios 的EditBox点击空白处不隐藏的解决方案

    原因:参数少了前缀CC 解决方案:修改 cocos/platform/ios/CCEAGLView-ios.mm 中的 handleTouchesAfterKeyboardShow -(void) h ...

  4. 《C++ Primer Plus》第3章 处理数据 学习笔记

    C++的基本类型分为两组:一组由存储为证书的值组成,另一组由存储为浮点格式的值组成.整型之间通过存储键值时使用的呢存及有无符号来区分.整型从最小到最大依次是:bool,char,signed char ...

  5. Socket通信编程实例(SIB和SS'SOB)

    客户端: package socket; import java.io.BufferedReader; import java.io.IOException; import java.io.Input ...

  6. poj_2479 动态规划

    题目大意 给定一列数,从中选择两个不相交的连续子段,求这两个连续子段和的最大值. 题目分析 典型的M子段和的问题,使用动态规划的方法来解决. f[i][j] 表示将A[1...i] 划分为j个不相交连 ...

  7. codevs 5965 [SDOI2017]新生舞会

    分数规划的裸题. 不会分数规划的OIer.百度:胡伯涛<最小割模型在信息学竞赛中的应用> /* TLE1: last:add(i,j+n,1e9,(real)((real)a[i][j]- ...

  8. c#基础 第三讲

    Random r = new Random();                 string x, y;             while (true)             {         ...

  9. android-修改TextView中部分文字的颜色

    :

  10. java中的最重要的 集合框架

    java.util这个重要的包包含大量的类和接口,支持很多的功能.例如,java.util具有能产生伪随机数的类,还包括可以管理日期和时间.观察事件.操作位集合.标记字符串.处理格式化数据等的类.ja ...