Java, Android 開發(fā)也有段時間了,當(dāng)初為了早點學(xué) Android,Java 匆匆了解個大概就結(jié)束了,基礎(chǔ)不夠扎實。
雖然集合框架經(jīng)常用,但是一直沒有仔細(xì)看看原理,僅止于會用,不知道為什么要這么做。
這段時間就開始 Java 集合的源碼學(xué)習(xí)。
public interface Iterable<T> { /** * Returns an {@link Iterator} for the elements in this object. * * @return An {@code Iterator} instance. */ Iterator<T> iterator();}
public interface Enumeration<E> {/** * Returns whether this {@code Enumeration} has more elements. * * @return {@code true} if there are more elements, {@code false} otherwise. * @see #nextElement */ public boolean hasMoreElements();/** * Returns the next element in this {@code Enumeration}. * * @return the next element.. * @throws NoSuchElementException * if there are no more elements. * @see #hasMoreElements */ public E nextElement();}
//StringTokenizer : 切割, Breaks a string into tokens; new code should probably use {@link String#split}. Enumeration enumeration = new StringTokenizer("A-B-C", "-"); while (enumeration.hasMoreElements()){ System.out.println(enumeration.nextElement()); }
運行結(jié)果:
NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
Iterators differ from enumerations in two ways:
- Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
- Method names have been improved.
public E next() { if (expectedModCount == modCount) { try { E result = get(pos + 1); lastPosition = ++pos; return result; } catch (IndexOutOfBoundsException e) { throw new NoSuchElementException(); } } throw new ConcurrentModificationException(); } public void remove() { if (this.lastPosition == -1) { throw new IllegalStateException(); } if (expectedModCount != modCount) { throw new ConcurrentModificationException(); } try { AbstractList.this.remove(lastPosition); } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); } expectedModCount = modCount; if (pos == lastPosition) { pos--; } lastPosition = -1; }
public boolean add(E object) { //... modCount++; return true;}public void clear() { if (size != 0) { //... modCount++; }}public boolean remove(Object object) { Object[] a = array; int s = size; if (object != null) { for (int i = 0; i < s; i++) { if (object.equals(a[i])) { //... modCount++; return true; } } } else { for (int i = 0; i < s; i++) { if (a[i] == null) { //... modCount++; return true; } } } return false;}
public void notifyChanged() { synchronized(mObservers) { // since onChanged() is implemented by the app, it could do anything, including // removing itself from {@link mObservers} - and that could cause problems if // an iterator is used on the ArrayList {@link mObservers}. // to avoid such problems, just march thru the list in the reverse order. for (int i = mObservers.size() - 1; i >= 0; i--) { mObservers.get(i).onChanged(); } }}
to avoid such problems, just march thru the list in the reverse order
for(int i=0; i<集合的大小;i++){ // ... }
Iterator iterator = list.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); }
https://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html
https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
http://www.cnblogs.com/dolphin0520/p/3933551.html
http://blog.csdn.net/chenssy/article/details/38151189
http://blog.csdn.net/mazhimazh/article/details/17730517
http://javarevisited.blogspot.jp/2014/04/for-each-loop-puzzle-in-java-example.html