java util包学习(5) Vector 源码分析
来源:互联网 发布:淘宝店铺页头装修教程 编辑:程序博客网 时间:2024/05/16 05:38
package java.util;public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{ protected Object[] elementData; protected int elementCount; protected int capacityIncrement;//增量 /** use serialVersionUID from JDK 1.0.2 for interoperability java身份证*/ private static final long serialVersionUID = -2767605614048989439L; //使用指定的初始容量和容量增量构造一个空的向量。 public Vector(int initialCapacity, int capacityIncrement) {super();//调用父类构造 个人认为这是一种规范 虽然 什么都不做吧 if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);this.elementData = new Object[initialCapacity];this.capacityIncrement = capacityIncrement; } public Vector(int initialCapacity) {this(initialCapacity, 0); } public Vector() {this(10);//建议自己设置的时候大一些。。 } public Vector(Collection<? extends E> c) {elementData = c.toArray();elementCount = elementData.length;// c.toArray might (incorrectly) not return Object[] (see 6260652)if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, elementCount, Object[].class); } //同步方法 复制到另一个数组中 public synchronized void copyInto(Object[] anArray) {System.arraycopy(elementData, 0, anArray, 0, elementCount); }/*对此向量的容量进行微调,使其等于向量的当前大小。如果此向量的容量大于其当前大小,则通过将其内部数据数组(保存在字段 elementData 中)替换为一个较小的数组,从而将容量更改为等于当前大小。应用程序可以使用此操作最小化向量的存储。 */ public synchronized void trimToSize() {modCount++;int oldCapacity = elementData.length;if (elementCount < oldCapacity) { elementData = Arrays.copyOf(elementData, elementCount);} } public synchronized void ensureCapacity(int minCapacity) {modCount++;ensureCapacityHelper(minCapacity);// 增加此向量的容量(如有必要),以确保其至少能够保存最小容量参数指定的组件数。 } private void ensureCapacityHelper(int minCapacity) {int oldCapacity = elementData.length;if (minCapacity > oldCapacity) { Object[] oldData = elementData; int newCapacity = (capacityIncrement > 0) ?(oldCapacity + capacityIncrement) : (oldCapacity * 2); if (newCapacity < minCapacity) {newCapacity = minCapacity; } elementData = Arrays.copyOf(elementData, newCapacity);} } public synchronized void setSize(int newSize) {modCount++;if (newSize > elementCount) { ensureCapacityHelper(newSize);} else { for (int i = newSize ; i < elementCount ; i++) {elementData[i] = null; }}elementCount = newSize; } public synchronized int capacity() {return elementData.length; } public synchronized int size() {return elementCount; } public synchronized boolean isEmpty() {return elementCount == 0; } public Enumeration<E> elements() {return new Enumeration<E>() { int count = 0; public boolean hasMoreElements() {return count < elementCount; } public E nextElement() {synchronized (Vector.this) { if (count < elementCount) {return (E)elementData[count++]; }}throw new NoSuchElementException("Vector Enumeration"); }}; } public boolean contains(Object o) {return indexOf(o, 0) >= 0; } public int indexOf(Object o) {return indexOf(o, 0); } public synchronized int indexOf(Object o, int index) {if (o == null) { for (int i = index ; i < elementCount ; i++)if (elementData[i]==null) return i;} else { for (int i = index ; i < elementCount ; i++)if (o.equals(elementData[i])) return i;}return -1; } public synchronized int lastIndexOf(Object o) {return lastIndexOf(o, elementCount-1); } public synchronized int lastIndexOf(Object o, int index) { if (index >= elementCount) throw new IndexOutOfBoundsException(index + " >= "+ elementCount);if (o == null) { for (int i = index; i >= 0; i--)if (elementData[i]==null) return i;} else { for (int i = index; i >= 0; i--)if (o.equals(elementData[i])) return i;}return -1; } public synchronized E elementAt(int index) {if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);//超过了数组已有数据的范围 当然越界了} return (E)elementData[index]; } public synchronized E firstElement() {if (elementCount == 0) { throw new NoSuchElementException();}return (E)elementData[0]; } public synchronized E lastElement() {if (elementCount == 0) { throw new NoSuchElementException();}return (E)elementData[elementCount - 1]; } public synchronized void setElementAt(E obj, int index) {if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);}elementData[index] = obj; } public synchronized void removeElementAt(int index) {modCount++;if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);}else if (index < 0) { throw new ArrayIndexOutOfBoundsException(index);}int j = elementCount - index - 1;if (j > 0) { System.arraycopy(elementData, index + 1, elementData, index, j);//部分数据往前移}elementCount--;elementData[elementCount] = null; /* to let gc do its work */ } public synchronized void insertElementAt(E obj, int index) {modCount++;if (index > elementCount) { throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount);}ensureCapacityHelper(elementCount + 1);System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);//部分数据往后移elementData[index] = obj;elementCount++; } public synchronized void addElement(E obj) {modCount++;ensureCapacityHelper(elementCount + 1);elementData[elementCount++] = obj; } public synchronized boolean removeElement(Object obj) {modCount++;int i = indexOf(obj);if (i >= 0) { removeElementAt(i); return true;}return false; } public synchronized void removeAllElements() { modCount++;// Let gc do its workfor (int i = 0; i < elementCount; i++) elementData[i] = null;elementCount = 0; } public synchronized Object clone() {try { Vector<E> v = (Vector<E>) super.clone(); v.elementData = Arrays.copyOf(elementData, elementCount); v.modCount = 0; return v;} catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError();} } public synchronized Object[] toArray() { return Arrays.copyOf(elementData, elementCount); } public synchronized <T> T[] toArray(T[] a) { if (a.length < elementCount) return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());System.arraycopy(elementData, 0, a, 0, elementCount); if (a.length > elementCount) a[elementCount] = null; return a; } public synchronized E get(int index) {if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index);return (E)elementData[index]; } public synchronized E set(int index, E element) {if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index);Object oldValue = elementData[index];elementData[index] = element;return (E)oldValue; } public synchronized boolean add(E e) {modCount++;ensureCapacityHelper(elementCount + 1);elementData[elementCount++] = e; return true; } public boolean remove(Object o) { return removeElement(o); } public void add(int index, E element) { insertElementAt(element, index);//原来是插入 } public synchronized E remove(int index) {modCount++;if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index);Object oldValue = elementData[index];int numMoved = elementCount - index - 1;if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved);elementData[--elementCount] = null; // Let gc do its workreturn (E)oldValue; } public void clear() { removeAllElements(); } public synchronized boolean containsAll(Collection<?> c) { return super.containsAll(c); } public synchronized boolean addAll(Collection<? extends E> c) {modCount++; Object[] a = c.toArray(); int numNew = a.length;ensureCapacityHelper(elementCount + numNew); System.arraycopy(a, 0, elementData, elementCount, numNew); elementCount += numNew;return numNew != 0; } public synchronized boolean removeAll(Collection<?> c) { return super.removeAll(c); } public synchronized boolean retainAll(Collection<?> c) { return super.retainAll(c); } public synchronized boolean addAll(int index, Collection<? extends E> c) {modCount++;if (index < 0 || index > elementCount) throw new ArrayIndexOutOfBoundsException(index); Object[] a = c.toArray();int numNew = a.length;ensureCapacityHelper(elementCount + numNew);int numMoved = elementCount - index;if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew);elementCount += numNew;return numNew != 0; } public synchronized boolean equals(Object o) { return super.equals(o); } public synchronized int hashCode() { return super.hashCode(); } public synchronized String toString() { return super.toString(); } public synchronized List<E> subList(int fromIndex, int toIndex) { return Collections.synchronizedList(super.subList(fromIndex, toIndex), this); } protected synchronized void removeRange(int fromIndex, int toIndex) {modCount++;int numMoved = elementCount - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved);// Let gc do its workint newElementCount = elementCount - (toIndex-fromIndex);while (elementCount != newElementCount) elementData[--elementCount] = null; } private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {s.defaultWriteObject(); }}
代码看起来很长 其实很简单 就是一堆同步方法吧,,Vector 虽然不常用 但是 它是线程安全的 版本很老的一个类了
- java util包学习(5) Vector 源码分析
- java util包学习(4) Arrays 源码分析
- java util包学习(6) LinkedLIst 源码分析
- java util包学习(7)Set 源码分析
- java util包学习(8)Map源码分析
- java util包学习(9)HashMap源码分析
- java.util.concurrent 源码分析-包结构
- java.lang.UnsupportedOperationException(源码学习 Util包 Arrays)
- JDK源码分析——Java.util.Vector的浅析
- JDK源码分析——Java.util.Vector的浅析
- java util包学习(3) ArrayList LInkedList 分析
- java.util.Vector源码解析
- ConcurrentModificationException(源码学习 Util包 集合)
- JAVA 集合类(java.util)源码阅读笔记------Vector
- java.util.concurrent 包源码分析之线程池
- java.util.concurrent 包源码分析之XXQueue
- java.util.concurrent 包源码分析之线程同步辅助
- java.util.concurrent 包源码分析之Fork/Join框架
- Documentation/blockdev/ramdisk
- win 7 蓝屏、清理注册表、杀毒软件
- 工作与生活
- 要开始了。。。
- Smart Hands-Eclipse Debug的一些用法--图文
- java util包学习(5) Vector 源码分析
- 大连理工大学电信学部CSDN高校俱乐部新学期见面会成功举办
- hdu-2156-分数矩阵
- 仿android系统Launcher界面
- Ubuntu下安装Sublime Text 2
- String之超大数加法
- 【一】android studio安装
- code blocks中文字符输出乱码
- Java 面试题(基础篇1)