Java Reference
In-Depth Information
Output
Dick Harry Juliet Nina Tom
Expected: Dick Harry Juliet Nina Tom
S
ELF
C
HECK
1.
Do linked lists take more storage space than arrays of the same size?
2.
Why don't we need iterators with arrays?
A
DVANCED
T
OPIC
15.1: The Iterable Interface and the
ÑFor EachÒ Loop
You can use the Ȓfor eachȓ loop
for (Type variable : collection)
670
671
with any of the collection classes in the standard Java library. This includes the
ArrayList
and
LinkedList
classes as well as the library classes which will
be discussed in
Chapter 16
. In fact, the Ȓfor eachȓ loop can be used with any class
that implements the
Iterable
interface:
public interface Iterable<E>
{
Iterator<E> iterator();
}
The interface has a type parameter
E
, denoting the element type of the collection.
The single method,
iterator
, yields an object that implements the
Iterator
interface.
public interface Iterator<E>
{
boolean hasNext();
E next();
void remove();
}