Thinking about using our OCA 8 book to study for the Java Foundations Junior Associate exam? It covers most of the topics. See what other topics you need to learn and where to read about that. One of those topics is iterating through a list.
Since Java 5, the most common way to iterate though a list (if you don’t need the loop index) is:
List<String> list = Arrays.asList("sheep", "deer", "rat");
for (String name : list) {
System.out.println(name);
}
Before Java 5 came along, there was another way:
List list = Arrays.asList("sheep", "deer", "rat");
Iterator it = list.iterator();
while (it.hasNext()) {
String name = (String) it.next();
System.out.println(name);
}
But we are told to use generics in new code which would give us:
List<String> list = Arrays.asList("sheep", "deer", "rat");
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String name = it.next();
System.out.println(name);
}
Why would you do this? Shrug. For reading old code I guess. But it is on the test so no time like the present to learn this idiom. See what is wrong here?
// DOES NOT COMPILE
List<String> list = Arrays.asList("sheep", "deer", "rat");
Iterator<String> it = list.iterator();
while (it.next()) {
String name = it.hasNext();
System.out.println(name);
}
This one reverses the order of hasNext/next. Remember that you have to check that the iterator has a next element before getting it. Now what do you think is the problem with this?
// BAD
List<String> list = Arrays.asList("sheep", "deer", "rat");
Iterator<String> it = list.iterator();
System.out.println(it.next());
If the list is empty, this code throws an exception. Probably not what you had in mind. Instead, you should write:
List<String> list = Arrays.asList("sheep", "deer", "rat");
Iterator<String> it = list.iterator();
if (it.hasNext()) System.out.println(it.next());
Summary
There’s not much you have to memorize. The key facts are:
- Call hasNext() in an if statement or while loop
- Call next() once you know there is a next element
- If the Iterator doesn’t use generics, you must cast unless you want Object
Practice Questions
Question 1
Which correctly fill in the blanks?
List<String> list = Arrays.asList("a", "b", "c");
Iterator<String> it = list.iterator();
while (it._____()) {
String name = it.______();
System.out.println(name);
}
A: hasNext, next
B: next, hasNext
C: The code does not compile with either A or B
D: The code throws an exception after being completed with A or B
Question 2
Which correctly fill in the blanks?
List<String> list = Arrays.asList("a", "b", "c");
Iterator it = list.iterator();
while (it._____()) {
String name = it.______();
System.out.println(name);
}
A: hasNext, next
B: next, hasNext
C: The code does not compile with either A or B
D: The code throws an exception after being completed with A or B
Question 3
Which correctly fill in the blanks?
List list = Arrays.asList("a", "b", "c");
Iterator<String> it = list.iterator();
while (it._____()) {
String name = it.______();
System.out.println(name);
}
A: hasNext, next
B: next, hasNext
C: The code does not compile with either A or B
D: The code throws an exception after being completed with A or B
The first line of the second code snippet seems incorrect: exactly because generics were introduced in Java 5, this line — List list = Arrays.asList(“sheep”, “deer”, “rat”); — won’t compile with ver.1.4 and earlier.
It won’t compile because it had generics in it! I’ve removed the generics. Thanks.
On the questions the while statements require the `it.` iterator
You are correct. Fixed.
The last code snippet (just before summary) misses a round bracket for if construct.
@Denis: Confirmed and fixed
codes will not compile due to missing imports
@mr that’s a code snippet so it doesn’t need imports
Hi there, the text is appearing incorrectly and I just can’t follow it no matter how hard I try haha. I’ve uploaded an image of how it appears for me: https://imgur.com/a/OMZVBdr
WordPress must have changed things since that was written. I’ve updated/reformatted this post.