Wednesday, February 23, 2011

Chapter 44: Using Lists

We saw what a list is and how useful it is and stuff like that. In this chapter we are going to take a look at how we should use them. So without any further delays, lets get started!!!

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 d = new ArrayList();
Car Car = new Car("BMW");
d.add(new Car("Ferrari"));
d.add(new Car("Porsche"));
Iterator < car > i3 = d.iterator(); // make an iterator
while (i3.hasNext()) {
Car d2 =; // cast not required
System.out.println("size " + d.size());
System.out.println("get1 " + d.get(1).name);
System.out.println("BMW " + d.indexOf(Car));
Object[] obj = d.toArray();
for(Object o : obj) {
Car d2 = (Car)o;
System.out.println("obj " +;

This produces
size 3
get1 Ferrari
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);

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


  1. where have you used generics for iterator? I don't understand....

  2. @ Michee - check out the iterator i3. There it is a generic iterator based on the objects contained in the list.


  3. i don't see any difference between:

    Iterator 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);

  4. @ Michee

    Nice 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


© 2013 by All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.