今天看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);//是否包括oIterator<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; }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); }/** *看数组里面的元素和数组本身的长度,如果size<length就可以缩小 * */public void trimToSize() {modCount++;//这个到底是干什么用的?int oldCapacity = elementData.length;if (size < oldCapacity) {Object oldData[] = elementData;elementData = (E[])new Object;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;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==null)return i;} else {for (int i = 0; i < size; i++)if (elem.equals(elementData))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==null)return i;} else {for (int i = size-1; i >= 0; i--)if (elem.equals(elementData))return i;}return -1; }//克隆//先复制这个对象,再复制这个对象中数组对象public Object clone() {try { ArrayList<E> v = (ArrayList<E>) super.clone();v.elementData = (E[])new Object;System.arraycopy(elementData, 0, v.elementData, 0, size);v.modCount = 0;return v;} catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneablethrow new InternalError();} }public Object[] toArray() {Object[] result = new Object;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 = null;} return a; }//获得public E get(int index) {RangeCheck(index);//看是否超出size的大小return elementData; }//设置public E set(int index, E element) {RangeCheck(index);E oldValue = elementData;elementData = element;return oldValue; }//增加一个对象public boolean add(E o) {ensureCapacity(size + 1);// Increments modCount!!elementData = 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 = element;size++; }public E remove(int index) {RangeCheck(index);modCount++;E oldValue = elementData;int numMoved = size - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index, numMoved);elementData[--size] = null; // Let gc do its workreturn oldValue; }public boolean remove(Object o) {if (o == null) {for (int index = 0; index < size; index++)if (elementData == null) {fastRemove(index);return true;}} else {for (int index = 0; index < size; index++)if (o.equals(elementData)) {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 workfor (int i = 0; i < size; i++){elementData = null;}size = 0; }public boolean addAll(Collection<? extends E> c) {Object[] a = c.toArray();int numNew = a.length;ensureCapacity(size + numNew);// Increments modCountSystem.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 modCountint 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 workint 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 stuffs.defaultWriteObject();// Write out array lengths.writeInt(elementData.length);// Write out all elements in the proper order.for (int i=0; i<size; i++){s.writeObject(elementData);}if (modCount != expectedModCount) {throw new ConcurrentModificationException();} }private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException {// Read in size, and any hidden stuffs.defaultReadObject();// Read in array length and allocate arrayint arrayLength = s.readInt();Object[] a = elementData = (E[])new Object;// Read in all elements in the proper order.for (int i=0; i<size; i++){a = 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;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;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;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 = 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; }}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==null)return i;} else {for (int i = index ; i < elementCount ; i++)if (elem.equals(elementData))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==null)return i;} else {for (int i = index; i >= 0; i--)if (elem.equals(elementData))return i;}return -1;}public synchronized E elementAt(int index) {if (index >= elementCount) {throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);} return (E)elementData; }//........................................}public class Stack<E> extends Vector<E> {public Stack() { }public E push(E item) {addElement(item);//父类的方法return item; }public synchronized E pop() {E obj;intlen = size();obj = peek();removeElementAt(len - 1);return obj; }public synchronized E peek() {intlen = 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]