1. public class Vector<E>
  2. extends AbstractList<E>
  3. implements List<E>, RandomAccess, Cloneable, java.io.Serializable

实例变量

  1. //保存元素的容器
  2. protected Object[] elementData;
  3.  
  4. //元素的数量
  5. protected int elementCount;
  6.  
  7. //容器扩容时的增量
  8. protected int capacityIncrement;

4个构造器

  1. //初始容量和容器扩容增量的构造器
  2. public Vector(int initialCapacity, int capacityIncrement) {
  3. super();
  4. if (initialCapacity < 0)
  5. throw new IllegalArgumentException("Illegal Capacity: "+
  6. initialCapacity);
  7. this.elementData = new Object[initialCapacity];
  8. this.capacityIncrement = capacityIncrement;
  9. }
  10.  
  11. //只有初始容量的构造器,默认使用容器扩容增量为0,表示不指定,那么扩容时会变成原容量的2倍
  12. public Vector(int initialCapacity) {
  13. this(initialCapacity, 0);
  14. }
  15.  
  16. 不指定初始容量,那么默认初始容量为10
  17. public Vector() {
  18. this(10);
  19. }
  20.  
  21. //使用c构造Vector
  22. public Vector(Collection<? extends E> c) {
  23. elementData = c.toArray();
  24. elementCount = elementData.length;
  25. // c.toArray might (incorrectly) not return Object[] (see 6260652)
  26. if (elementData.getClass() != Object[].class)
  27. elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
  28. }

扩容方法

  1. public synchronized void ensureCapacity(int minCapacity) {
  2. if (minCapacity > 0) {
  3. modCount++;
  4. ensureCapacityHelper(minCapacity);
  5. }
  6. }
  7.  
  8. private void ensureCapacityHelper(int minCapacity) {
  9. // overflow-conscious code
  10. if (minCapacity - elementData.length > 0)
  11. grow(minCapacity);
  12. }
  13.  
  14. private void grow(int minCapacity) {
  15. // overflow-conscious code
  16. int oldCapacity = elementData.length;
  17. int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
  18. capacityIncrement : oldCapacity);//如果capacityIncrement为0,那么newCapacity为oldCapacity * 2
  19. if (newCapacity - minCapacity < 0)
  20. newCapacity = minCapacity;
  21. if (newCapacity - MAX_ARRAY_SIZE > 0)
  22. newCapacity = hugeCapacity(minCapacity);
  23. elementData = Arrays.copyOf(elementData, newCapacity);
  24. }

设置容量

  1. public synchronized void setSize(int newSize) {
  2. modCount++;
  3. if (newSize > elementCount) {//如果新容量大于旧容量,扩容
  4. ensureCapacityHelper(newSize);
  5. } else {//否则,将超过新容量的部分都设置为null,注意容量没变,只是超出newSize的元素变成null
  6. for (int i = newSize ; i < elementCount ; i++) {
  7. elementData[i] = null;
  8. }
  9. }
  10. elementCount = newSize;
  11. }

2个迭代器,一个从头迭代到尾,一个从尾迭代到头

  1. private class Itr implements Iterator<E> {//正向迭代器,cursor为0
  2. int cursor; // index of next element to return
  3. int lastRet = -1; // index of last element returned; -1 if no such
  4. int expectedModCount = modCount;
  5.  
  6. public boolean hasNext() {
  7. // Racy but within spec, since modifications are checked
  8. // within or after synchronization in next/previous
  9. return cursor != elementCount;
  10. }
  11.  
  12. public E next() {
  13. synchronized (Vector.this) {
  14. checkForComodification();
  15. int i = cursor;
  16. if (i >= elementCount)
  17. throw new NoSuchElementException();
  18. cursor = i + 1;
  19. return elementData(lastRet = i);
  20. }
  21. }
  22.  
  23. public void remove() {
  24. if (lastRet == -1)
  25. throw new IllegalStateException();
  26. synchronized (Vector.this) {
  27. checkForComodification();
  28. Vector.this.remove(lastRet);
  29. expectedModCount = modCount;
  30. }
  31. cursor = lastRet;
  32. lastRet = -1;
  33. }
  34.  
  35. @Override
  36. public void forEachRemaining(Consumer<? super E> action) {
  37. Objects.requireNonNull(action);
  38. synchronized (Vector.this) {
  39. final int size = elementCount;
  40. int i = cursor;
  41. if (i >= size) {
  42. return;
  43. }
  44. @SuppressWarnings("unchecked")
  45. final E[] elementData = (E[]) Vector.this.elementData;
  46. if (i >= elementData.length) {
  47. throw new ConcurrentModificationException();
  48. }
  49. while (i != size && modCount == expectedModCount) {
  50. action.accept(elementData[i++]);
  51. }
  52. // update once at end of iteration to reduce heap write traffic
  53. cursor = i;
  54. lastRet = i - 1;
  55. checkForComodification();
  56. }
  57. }
  58.  
  59. final void checkForComodification() {
  60. if (modCount != expectedModCount)
  61. throw new ConcurrentModificationException();
  62. }
  63. }
  1. final class ListItr extends Itr implements ListIterator<E> {//从index到头迭代
  2. ListItr(int index) {
  3. super();
  4. cursor = index;
  5. }
  6.  
  7. public boolean hasPrevious() {
  8. return cursor != 0;
  9. }
  10.  
  11. public int nextIndex() {
  12. return cursor;
  13. }
  14.  
  15. public int previousIndex() {
  16. return cursor - 1;
  17. }
  18.  
  19. public E previous() {
  20. synchronized (Vector.this) {
  21. checkForComodification();
  22. int i = cursor - 1;
  23. if (i < 0)
  24. throw new NoSuchElementException();
  25. cursor = i;
  26. return elementData(lastRet = i);
  27. }
  28. }
  29.  
  30. public void set(E e) {
  31. if (lastRet == -1)
  32. throw new IllegalStateException();
  33. synchronized (Vector.this) {
  34. checkForComodification();
  35. Vector.this.set(lastRet, e);
  36. }
  37. }
  38.  
  39. public void add(E e) {
  40. int i = cursor;
  41. synchronized (Vector.this) {
  42. checkForComodification();
  43. Vector.this.add(i, e);
  44. expectedModCount = modCount;
  45. }
  46. cursor = i + 1;
  47. lastRet = -1;
  48. }
  49. }
  50.  
  51. @Override
  52. public synchronized void forEach(Consumer<? super E> action) {
  53. Objects.requireNonNull(action);
  54. final int expectedModCount = modCount;
  55. @SuppressWarnings("unchecked")
  56. final E[] elementData = (E[]) this.elementData;
  57. final int elementCount = this.elementCount;
  58. for (int i=0; modCount == expectedModCount && i < elementCount; i++) {
  59. action.accept(elementData[i]);
  60. }
  61. if (modCount != expectedModCount) {
  62. throw new ConcurrentModificationException();
  63. }
  64. }
  1. //index指定从哪个位置开始往前迭代
  2. public synchronized ListIterator<E> listIterator(int index) {
  3. if (index < 0 || index > elementCount)
  4. throw new IndexOutOfBoundsException("Index: "+index);
  5. return new ListItr(index);
  6. }

Vector是线程安全的,它的方法使用synchronized修饰,如果不需要线程安全,推荐使用ArrayList代替。

java.util.Vector的更多相关文章

  1. 【转】java.util.vector中的vector的详细用法

    [转]java.util.vector中的vector的详细用法 ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.uti ...

  2. java.util.vector中的vector的详细用法

    ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.util.*; /** * 演示Vector的使用.包括Vector的创 ...

  3. java.util.ArrayList、java.util.vector和java.util.LinkedList (JDK 1.8.0_111)

    一.java.util.ArrayList 1.1 ArrayList 继承结构 ArrayList实现了RandomAccess,可以随机访问(其实就是通过数组下标访问):实现了Cloneable, ...

  4. java.util.Vector排序

    Vector的排序: import java.util.*; class MyCompare implements Comparator //实现Comparator,定义自己的比较方法{public ...

  5. java.util.ArrayList,java.util.LinkedList,java.util.Vector的区别,使用场合.

    下图是Collection的类继承图 从图中可以看出:Vector.ArrayList.LinkedList这三者都实现了List 接口.所有使用方式也很相似,主要区别在于实现方式的不同,所以对不同的 ...

  6. JDK源码阅读-------自学笔记(二十五)(java.util.Vector 自定义讲解)

    Vector 向量 Vector简述 1).Vector底层是用数组实现的List 2).虽然线程安全,但是效率低,所以并不是安全就是好的 3).底层大量方法添加synchronized同步标记,sy ...

  7. JDK1.8源码(五)——java.util.Vector类

    JDK1.8源码(五)--java.lang. https://www.cnblogs.com/IT-CPC/p/10897559.html

  8. Java中vector的使用详解

    Vector 可实现自动增长的对象数组. java.util.vector提供了向量类(vector)以实现类似动态数组的功能.在Java语言中没有指针的概念,但如果正确灵活地使用指针又确实可以大大提 ...

  9. java util包概述

    util是utiliy的缩写,意为多用途的,工具性质的包这个包中主要存放了:集合类(如ArrayList,HashMap等),随机数产生类,属性文件读取类,定时器类等类.这些类极大方便了Java编程, ...

随机推荐

  1. vm内核参数优化设置

     http://www.cnblogs.com/wjoyxt/archive/2014/06/08/3777042.html (1)vm.overcommit_memory 执行grep -i com ...

  2. 用komodo建立python开发环境

    配置 在菜单中选择Edit.Preferences. 代码自动完成 更改tab代表的空格数 括号自动关闭和语法检查 interactive shell 中文支持 缩写 点击菜单View.Tabs &a ...

  3. C++ atol

    函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr);   简介编辑 相关函数: atof,atoi,strtod,strtol,st ...

  4. find the nth digit

    Problem Description 假设:S1 = 1S2 = 12S3 = 123S4 = 1234.........S9 = 123456789S10 = 1234567891S11 = 12 ...

  5. solr英文使用的基本分词器和过滤器配置

    solr英文应用的基本分词器和过滤器配置 英文应用分词器和过滤器一般配置顺序 索引(index): 1:空格 WhitespaceTokenizer    2:过滤词(停用词,如:on.of.a.an ...

  6. 《MFC游戏开发》笔记九 游戏中的碰撞判定初步&怪物运动简单AI

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9374935 作者:七十一雾央 新浪微博:http:// ...

  7. Read / Write Excel file in Java using Apache POI

    Read / Write Excel file in Java using Apache POI 2014-04-18 BY DINESH LEAVE A COMMENT About a year o ...

  8. java实现https,https接口请求

    /**********************https 接口'*******************/ /** * 安全证书管理器 */public class MyX509TrustManager ...

  9. 剑指Offer44 扑克牌的顺子

    /************************************************************************* > File Name: 44_Contin ...

  10. xcode6 真机运行报错 Command /usr/bin/codesign failed with exit code 1

    解决方法: 百度下载‘iphone配置实用工具’, 打开此软件之后,选择预配置描述文件, 选中iphone过期和重复的描述文件,按delete键删除.然后重启xcode即可