Datasheet

However, the compiler expands the array version to code slightly longer than the collection version:
String[] $strArray = stringArray;
for(int $i = 0; $i < $strArray.length; $i++) {
String strObject = $strArray[$i];
// ... statements here ...
}
The compiler this time uses two temporary and unique variables during the expansion. The first is an
alias to the array, and the second is the loop counter.
Additions to the Java Class Library
To fully support the new for loop syntax, the object iterated over must be an array or inherit from a new
interface,
java.lang.Iterable, directly or indirectly. The existing collection classes were retrofitted for
the release of JDK 5. The new
Iterable interface looks like this:
public interface Iterable {
/**
* Returns an iterator over the elements in this collection. There are no
* guarantees concerning the order in which the elements are returned
* (unless this collection is an instance of some class that provides a
* guarantee).
*
* @return an Iterator over the elements in this collection.
*/
SimpleIterator iterator();
}
Additionally, java.util.Iterator will be retrofitted to implement java.lang.ReadOnlyIterator,
as shown here:
public interface ReadOnlyIterator {
/**
* Returns true if the iteration has more elements. (In other
* words, returns true if next would return an element
* rather than throwing an exception.)
*
* @return true if the iterator has more elements.
*/
boolean hasNext();
/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration.
* @exception NoSuchElementException iteration has no more elements.
*/
Object next();
}
17
Chapter 1: Key Java Language Features and Libraries
05_777106 ch01.qxp 11/28/06 10:43 PM Page 17