`

今天看JDK的一些原码,头都大了。。。

阅读更多
public interface Iterator<E> {
	 boolean hasNext();		//是否有下一个元素
	 E next();				//下一个元素
	 void remove();			//删除
}

public interface Collection<E> extends Iterable<E> {
	int size();							//包函的元素
	boolean isEmpty();					//是否为空
	boolean contains(Object o);			//是否包括o
	Iterator<E> iterator();				//生成Iterator对象
	Object[] toArray();					//生成数组对象
	<T> T[] toArray(T[] a);				//
	boolean add(E o);					//加一个对象
	boolean remove(Object o);			//删除一个对象
	boolean containsAll(Collection<?> c);			//
	boolean addAll(Collection<? extends E> c);		//添加所有的到当前集合,包括extends E的
	boolean removeAll(Collection<?> c);				//
	boolean retainAll(Collection<?> c);				//
	void clear();									//清除
	boolean equals(Object o);						//
	int hashCode();									//
}

public interface List<E> extends Collection<E> {
	 int size();
	 boolean isEmpty();
	 boolean contains(Object o);
	 Iterator<E> iterator();
	 Object[] toArray();
	 <T> T[] toArray(T[] a);
	 boolean add(E o);
	 boolean remove(Object o);
	 boolean containsAll(Collection<?> c);
	 boolean addAll(Collection<? extends E> c);
	 boolean addAll(int index, Collection<? extends E> c);
	 boolean removeAll(Collection<?> c);
	 boolean retainAll(Collection<?> c);
	 void clear();
	 boolean equals(Object o);
	 int hashCode();
	 //List接口里面自己的方法
	 E get(int index);									//根据index值出相应的对象
	 E set(int index, E element);						//设置相应位置的对象
	 void add(int index, E element);					//增加相应位置的对象
	 E remove(int index);								//删除
	 int indexOf(Object o);								//根据对象获得相应对象的位置,没有找到应该会是-1
	 int lastIndexOf(Object o);							//是最后一个开始找吧(不知道)
	 ListIterator<E> listIterator();					//成生ListIterator对象,链表实现的吧
	 ListIterator<E> listIterator(int index);			//成生ListIterator对象,从index开始生成
	 List<E> subList(int fromIndex, int toIndex);		//从fromIndex到toIndex生成新的List对象
}

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
	private static final long serialVersionUID = 8683452581122892189L;				//我不知道为什么会有一个这样的变量
	private transient E[] elementData;												//用来保存E的数组,ArrayList是数组实现的
	private int size;																//指这个数组保存的个数,并不是数组的大小
	private int modCount;

	//构造方法	
	public ArrayList(int initialCapacity) {
		super();
        if (initialCapacity < 0){
            throw new IllegalArgumentException("Illegal Capacity: "+initialCapacity);
		}
		this.elementData = (E[])new Object[initialCapacity];
    }

	public ArrayList() {
		this(10);
    }
	
	public ArrayList(Collection<? extends E> c) {
        size = c.size();
        // Allow 10% room for growth
        int capacity = (int)Math.min((size*110L)/100, Integer.MAX_VALUE);
        elementData = (E[])c.toArray(new Object[capacity]);
    }
	
	/**
	 *	看数组里面的元素和数组本身的长度,如果size<length就可以缩小
	 *
	 */
	public void trimToSize() {
		modCount++;														//这个到底是干什么用的?
		int oldCapacity = elementData.length;
		if (size < oldCapacity) {
			Object oldData[] = elementData;
			elementData = (E[])new Object[size];
			System.arraycopy(oldData, 0, elementData, 0, size);
		}
    }
	//扩大容量
	public void ensureCapacity(int minCapacity) {
		modCount++;
		int oldCapacity = elementData.length;
		if (minCapacity > oldCapacity) {
			Object oldData[] = elementData;
			int newCapacity = (oldCapacity * 3)/2 + 1;				//JDK1.5是这样写的哦,不知道为什么?
			if (newCapacity < minCapacity){
				newCapacity = minCapacity;							//他的容量并一定扩大到minCapacity,满足条件才会扩大到那样
			}
			elementData = (E[])new Object[newCapacity];
			System.arraycopy(oldData, 0, elementData, 0, size);
		}
    }

	public int size() {
		return size;
    }

	public boolean isEmpty() {
		return size == 0;
    }
	//查个某个对象在数组中的位置,是否相于根据对象的equals方法
	public int indexOf(Object elem) {
		if (elem == null) {
			for (int i = 0; i < size; i++)
			if (elementData[i]==null)
				return i;
		} else {
			for (int i = 0; i < size; i++)
			if (elem.equals(elementData[i]))
				return i;
		}
		return -1;
    }
	//这个方法和上面一个意思
	public boolean contains(Object elem) {
		return indexOf(elem) >= 0;
    }
	//很明显这个方法是从后向前找的
	public int lastIndexOf(Object elem) {
		if (elem == null) {
			for (int i = size-1; i >= 0; i--)
			if (elementData[i]==null)
				return i;
		} else {
			for (int i = size-1; i >= 0; i--)
			if (elem.equals(elementData[i]))
				return i;
		}
		return -1;
    }
	//克隆
	//先复制这个对象,再复制这个对象中数组对象
	public Object clone() {
		try { 
			ArrayList<E> v = (ArrayList<E>) super.clone();
			v.elementData = (E[])new Object[size];
			System.arraycopy(elementData, 0, v.elementData, 0, size);
			v.modCount = 0;
			return v;
		} catch (CloneNotSupportedException e) { 
			// this shouldn't happen, since we are Cloneable
			throw new InternalError();
		}
    }
	
	public Object[] toArray() {
		Object[] result = new Object[size];
		System.arraycopy(elementData, 0, result, 0, size);
		return result;
    }
	//不知道什么意思。。。。
	public <T> T[] toArray(T[] a) {
        if (a.length < size){
            a = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
		}
		System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size){
            a[size] = null;
		}
        return a;
    }
	//获得
	public E get(int index) {
		RangeCheck(index);	//看是否超出size的大小
		return elementData[index];
    }
	//设置
	public E set(int index, E element) {
		RangeCheck(index);
		E oldValue = elementData[index];
		elementData[index] = element;
		return oldValue;
    }
	//增加一个对象
	public boolean add(E o) {
		ensureCapacity(size + 1);  // Increments modCount!!
		elementData[size++] = o;
		return true;
    }
	//在index这个位置后面加个对象进去
	public void add(int index, E element) {
		if (index > size || index < 0){
			throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
		}
		ensureCapacity(size+1);  // Increments modCount!!
		System.arraycopy(elementData, index, elementData, index + 1,
				 size - index);
		elementData[index] = element;
		size++;
    }

	public E remove(int index) {
		RangeCheck(index);
		modCount++;
		E oldValue = elementData[index];
		int numMoved = size - index - 1;
		if (numMoved > 0)
			System.arraycopy(elementData, index+1, elementData, index,
					 numMoved);
		elementData[--size] = null; // Let gc do its work
		return oldValue;
    }

	public boolean remove(Object o) {
		if (o == null) {
				for (int index = 0; index < size; index++)
			if (elementData[index] == null) {
				fastRemove(index);
				return true;
			}
		} else {
			for (int index = 0; index < size; index++)
			if (o.equals(elementData[index])) {
				fastRemove(index);
				return true;
			}
		}
		return false;
    }
	//private方法了
	private void fastRemove(int index) {
        modCount++;
        int numMoved = size - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index, 
                             numMoved);
        elementData[--size] = null; // Let gc do its work
    }

	public void clear() {
		modCount++;
		// Let gc do its work
		for (int i = 0; i < size; i++){
			elementData[i] = null;
		}			
		size = 0;
    }

	public boolean addAll(Collection<? extends E> c) {
		Object[] a = c.toArray();
		int numNew = a.length;
		ensureCapacity(size + numNew);  // Increments modCount
		System.arraycopy(a, 0, elementData, size, numNew);
		size += numNew;
		return numNew != 0;
    }

	public boolean addAll(int index, Collection<? extends E> c) {
		if (index > size || index < 0)
			throw new IndexOutOfBoundsException(
			"Index: " + index + ", Size: " + size);

		Object[] a = c.toArray();
		int numNew = a.length;
		ensureCapacity(size + numNew);  // Increments modCount

		int numMoved = size - index;
		if (numMoved > 0)
			System.arraycopy(elementData, index, elementData, index + numNew,
					 numMoved);

			System.arraycopy(a, 0, elementData, index, numNew);
		size += numNew;
		return numNew != 0;
    }

	protected void removeRange(int fromIndex, int toIndex) {
		modCount++;
		int numMoved = size - toIndex;
			System.arraycopy(elementData, toIndex, elementData, fromIndex,
							 numMoved);

		// Let gc do its work
		int newSize = size - (toIndex-fromIndex);
		while (size != newSize){
			elementData[--size] = null;
		}
    }
	
	private void RangeCheck(int index) {
		if (index >= size){
			throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);
		}
    }

	 private void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException{
		int expectedModCount = modCount;
		// Write out element count, and any hidden stuff
		s.defaultWriteObject();

			// Write out array length
			s.writeInt(elementData.length);

		// Write out all elements in the proper order.
		for (int i=0; i<size; i++){
				s.writeObject(elementData[i]);
		}

		if (modCount != expectedModCount) {
			throw new ConcurrentModificationException();
		}
    }

	private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
		// Read in size, and any hidden stuff
		s.defaultReadObject();

			// Read in array length and allocate array
			int arrayLength = s.readInt();
			Object[] a = elementData = (E[])new Object[arrayLength];

		// Read in all elements in the proper order.
		for (int i=0; i<size; i++){
				a[i] = s.readObject();
			}
		}
}
public class Vector<E> extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable{

	protected Object[] elementData;
	protected int elementCount;
	protected int capacityIncrement;

	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) {
        elementCount = c.size();
        // 10% for growth
        elementData = new Object[(int)Math.min((elementCount*110L)/100,Integer.MAX_VALUE)]; 
        c.toArray(elementData);
    }
	//把这个Vector对象的数组复制到别一个去
	public synchronized void copyInto(Object[] anArray) {
		System.arraycopy(elementData, 0, anArray, 0, elementCount);
    }
	
	public synchronized void trimToSize() {
		modCount++;
		int oldCapacity = elementData.length;
		if (elementCount < oldCapacity) {
			Object oldData[] = elementData;
			elementData = new Object[elementCount];
			System.arraycopy(oldData, 0, elementData, 0, 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 = new Object[newCapacity];
			System.arraycopy(oldData, 0, elementData, 0, elementCount);
		}
    }

	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 elem) {
		return indexOf(elem, 0) >= 0;
    }

	public int indexOf(Object elem) {
		return indexOf(elem, 0);
    }

	public synchronized int indexOf(Object elem, int index) {
		if (elem == null) {
			for (int i = index ; i < elementCount ; i++)
			if (elementData[i]==null)
				return i;
		} else {
			for (int i = index ; i < elementCount ; i++)
			if (elem.equals(elementData[i]))
				return i;
		}
		return -1;
    }

	public synchronized int lastIndexOf(Object elem) {
		return lastIndexOf(elem, elementCount-1);
    }

	public synchronized int lastIndexOf(Object elem, int index) {
        if (index >= elementCount)
            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
		if (elem == null) {
			for (int i = index; i >= 0; i--)
			if (elementData[i]==null)
				return i;
		} else {
			for (int i = index; i >= 0; i--)
			if (elem.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 class Stack<E> extends Vector<E> {
	public Stack() {

    }

	public E push(E item) {
		addElement(item);				//父类的方法
		return item;
    }

	public synchronized E pop() {
		E obj;
		int	len = size();

		obj = peek();
		removeElementAt(len - 1);

		return obj;
    }

	public synchronized E peek() {
		int	len = size();

		if (len == 0)
			throw new EmptyStackException();
		return elementAt(len - 1);
    }

	public boolean empty() {
		return size() == 0;
    }

	public synchronized int search(Object o) {
		int i = lastIndexOf(o);

		if (i >= 0) {
			return size() - i;
		}
		return -1;
    }
}
分享到:
评论
1 楼 hyxw5890 2009-02-04  
Google首席Java架构师自认为最自豪的代码就这么令人读起来头大吗?呵呵,开个玩笑

相关推荐

    jdk-8u60源码

    jdk 8u60 源码下载: 导入请阅读IMPORT_README Main: sun.misc.Launcher

    JDK1.7.0_71源码(包括保护的源码)

    JDK的源码,包括保护的源码

    JDK8源码及API文档.rar

    Java 8源码和Java 8 API文档(英语),多看源码和文档,学习优秀开发者的思维,研究语言底层的实现原理,对Java的学习有很大帮助。

    JDK13_SourceCode:JDK13源码阅读

    JDK13_SourceCode JDK13源码阅读只供阅读注释,无法直接调试

    java_jdk反编译工具

    反编译JAVA.class文件,特别是在没有原码地情况 ,适时地进行反编译,可以看到相关地原码!

    基于jdk1.8 的ArrayList的源码分析

    基于jdk1.8 的ArrayList的源码分析 前言:一说到ArrayList的大家可能立马想到的就是:有序、可重复、查找快但是增删慢、线程不安全。但是具体的原因都不是很清楚,本文就会根据这些问题和大家一起去学习。主要会从...

    struts2.1的一个最简单应用,myeclipse6.5原码

    struts2.1的一个最简单应用,Myeclipse6.5原码,tomcat6.0,jdk1.6

    从原码解析ArrayList

    1、介绍ArrayList底层代码 2、从源码讲解集合的安全失败与快速失败

    jdk-API1.6.zip

    API(Application Programming Interface,应用程序接口)是一些预先定义的函数,或指软件系统不同组成部分衔接的约定。 [1] 目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问...

    java开发毕业设计-基于SpringBoot+Vue的外卖点餐系统源码+数据库

    java毕业设计之外卖点餐系统(项目源码+...JDK版本:JDK1.8 服务器:tomcat7 数据库:mysql 5+(最好5.7版本) 数据库工具:Navicat11 开发软件:eclipse/myeclipse/idea Maven包:Maven3.3.9 浏览器:谷歌浏览器

    java毕业设计之新闻发布管理系统源码(前后端完整源代码).zip

    本系统前端主要功能包括新闻、公告的查看,在线留言等。后台主要功能包括新闻管理、留言管理、...JDK版本:JDK1.8 服务器:tomcat7+ 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件: idea Maven包:Maven3.3.9+

    JAVAweb客户管理系统源码eclipse版.rar

    javaweb客户系统管理项目,eclipse,ssh,mvc架构jdk1.7版本,非常适合新手可以二次开发哦

    JavaWeb期刊管理系统源代码+课程设计附课设报告.zip

    配置jdk版本17 修改数据库properties文件,改为你的数据库链接配置 配置你的tomcat(idea右上角添加运行配置,导入你的tomcat,然后idea会提示修正项目,修正即可,之后,上面会显示工程访问路径,点击就可以访问) ...

    安卓java读取网页源码-Poem:诗人

    JDK:JDK 1.8.0_144 Android SDK:API 28 tensorflow:1.11.0 python:2.7.5 服务端OS:centos-release-7-4.1708.el7 二、作品概述 简札 以诗词为基,以交流为体,赋予诗词更强的烟火气,使之走进普罗大众的手心。让...

    JAVA--达内培训笔记

    JAVA_HOME = /opt/jdk1.5.06 JDK 安装路径 --- JDK = JRE {JVM(硬件)+编译器(软件)} +编译器工具+类库 PATH = $Path:$Java_Home/bin:. ClassPath = . 类路径 7、包 --- 分类放置,减少命名空间 包名.类名 ...

    ssh(structs,spring,hibernate)框架中的上传下载

     •DefaultLobHandler:适用于大部分的数据库,如SqlServer,MySQL,对Oracle 10g也适用,但不适用于Oracle 9i(看来Oracle 9i确实是个怪胎,谁叫Oracle 公司自己都说Oracle 9i是一个过渡性的产品呢)。  •...

Global site tag (gtag.js) - Google Analytics