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; }
}
问题补充:
写得不错呀,几个模式和线程同步都到的,有啥问题吗?
=============================================
这是JDK的源码,用到什么模式了啊,说一说啥
问题补充:
哇,你太猛了,你知道怎么去看JDK的原代码么?那么多,没有方法是看不下去的哦。。。
问题补充:
那你介绍几本书哦,我想把J2SE的基础搞的好一点。。
我现在看的是Thanking Java 不过是讲的1.1版本的,看了两百多页就没有看下去了
还在看的是数据结构(JAVA写的)........
看了数据结构里面的链表才来看JDK里面的原码的,我以前就知道用,不知道里面是写的啥。。。