Java, Android 开发也有段时间了,当初为了早点学 Android,Java 匆匆了解个大概就结束了,基础不够扎实。 虽然集合框架经常用,但是一直没有仔细看看原理,仅止于会用,不知道为什么要这么做。 这段时间就开始 Java 集合的源码学习。
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());
}
运行结果:
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:
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