Using Lists
Remember that Lists are usually used to keep things in some kind of order. You can use a LinkedList to create a first-in, first-out queue. You can use an ArrayList to keep track of what locations were visited, and in what order. Notice that in both of these examples it’s perfectly reasonable to assume that duplicates might occur. In addition, Lists allow you to manually override the ordering of elements by adding or removing elements via the element’s index. Before Java 5, and the enhanced for loop, the most common way to examine a List “element by element” was by the use of an Iterator. You’ll still find Iterators in use in the Java code you encounter, and you might just find an Iterator or two on the exam. An Iterator is an object that’s associated with a specific collection. It lets you loop through the collection step by step.
The two Iterator methods you need to understand for the exam are
• boolean hasNext() Returns true if there is at least one more element in the collection being traversed. Invoking hasNext() does NOT move you to the next element of the collection.
• Object next() This method returns the next object in the collection, AND moves you forward to the element after the element just returned.
Let’s look at a little code that uses a List and an Iterator:
import java.util.*;
class Car {
public String name;
Car(String n) { name = n; }
}
class TestIteratorExample {
public static void main(String[] args) {
List
Car Car = new Car("BMW");
d.add(Car);
d.add(new Car("Ferrari"));
d.add(new Car("Porsche"));
Iterator < car > i3 = d.iterator(); // make an iterator
while (i3.hasNext()) {
Car d2 = i3.next(); // cast not required
System.out.println(d2.name);
}
System.out.println("size " + d.size());
System.out.println("get1 " + d.get(1).name);
System.out.println("BMW " + d.indexOf(Car));
d.remove(2);
Object[] obj = d.toArray();
for(Object o : obj) {
Car d2 = (Car)o;
System.out.println("obj " + d2.name);
}
}
}
This produces
BMW
Ferrari
Porsche
size 3
get1 Ferrari
BMW 0
obj BMW
obj Ferrari
First off, we used generics syntax to create the Iterator (an Iterator of type Car). Because of this, when we used the next() method, we didn’t have to cast the Object returned by next() to a Car. We could have declared the Iterator like this:
Iterator i3 = d.iterator(); // make an iterator
But then we would have had to cast the returned value:
Car d2 = (Car)i3.next();
The rest of the code demonstrates using the size(), get(), indexOf(), and toArray() methods. There shouldn’t be any surprises with these methods. As a last warning, remember that List is an interface!
Previous Chapter: Chapter 43 - Converting Arrays to List and List to Arrays
Next Chapter: Chapter 45 - Using Sets

where have you used generics for iterator? I don't understand....
ReplyDelete@ Michee - check out the iterator i3. There it is a generic iterator based on the objects contained in the list.
ReplyDeleteAnand
i don't see any difference between:
ReplyDeleteIterator i3 = d.iterator(); // make an iterator
and this at the end of the article:
"Iterator i3 = d.iterator(); // make an iterator
But then we would have had to cast the returned value:
Car d2 = (Car)i3.next();
"
@ Michee
ReplyDeleteNice catch and thanks for that. The blogger editor had removed the generic declaration < car > on the contents that got published.
Now I guess you can see the difference between the two iterators?
Thanks again
Anand