Java ArrayList and Iterator

Java is still fairly new to me, I have about 4 months exposure to it.

One thing that I like about the developer community is that there are various sources that can explain how to do something in anything.  I often finding myself reading someones blog whilst trying to figure something out.  So I’m returning the favour.

In this post, I will attempt (apologies if it sucks) to explain as much as I can on how to use the ArrayList in Java.  I’m sure you are aware what an ArrayList is, but if not heres a quick explanation.  A ArrayList is a one dimension array/stack which can contain Objects, it’s size (amount of objects it can contain) is not set but grows automatically. The size of the ArrayList can be set when you instantiate by using ArrayList(100). The ArrayList functions allow you to;

  • add(Object o) — Allows you to add objects.
  • get(int objectIndex) — Get an object using its index
  • size() — Get the size of the ArrayList.
  • remove(int objectIndex) — Remove an object from the ArrayList from the specified index.
  • indexOf(Object o) — Get the index of an Object.
  • clear() — Remove all objects from the ArrayList.

How to create an ArrayList

Here is an example taken from a recent uni assignment.  Lets say I sell Cars and in the application there is a Class called Car which represents a car that I may have.  Here is how we would populate an ArrayList with Car objects.

ArrayList carStock = new ArrayList<Car>;
Car toyotaYaris = new Car("MF54 XLL", "Petrol", "Blue", "Hatchback", 3500.0f);
carStock.add(toyotaYaris);

From the above example, you can see I created a new ArrayList called carStock.  This ArrayList will contain Car objects which is defined by <Car>.  The next step is to create a Car object which represents a Toyota Yaris, registration number: MF54 XLL, colour: Blue, fuel: Petrol and Costs £3,500.00.  Once this is created, the Object is then added to the ArrayList by calling the add() method.

Searching ArrayLists

Continuing on the Car example, this example will show how to get a Car out of the ArrayList using Iterator and the cars registration number.  This will be made into a function called getCar.  This function will return a Car object.

public function Car getCar(String registrationNumber){
  for (Iterator<Car> i = carStock.iterator(); i.hasNext();) {
    Car currentCar = (Car) i.next();
    if (currentCar.getRegNumber().equals(registrationNumber)) {
      return currentCar;
    }
  }
  return null;
}

This method takes a string represented by registrationNumber and will loop through the carStock ArrayList checking each time if the registration of the current car is equal to the one provided.  If the car matches, it will return the Car object.

The for loop shown above checks each cycle if the carStock iterator has another object using the hasNext() method.  This method will return a Boolean (true/false) value each cycle.  As long as the Boolean value is true, it will continue to cycle through the ArrayList.

So we have a new function, how do we use it?  Assuming somewhere else within the application we want to search for a car, the following code would be used;

Car foundCar = getCar("MF54 XLL");
system.out.println(foundCar.getRegistrationNumber());

This code will call the new method getCar() with the registration number “MF54 XLL” and set the object reference of foundCar to that contained in the ArrayList (carStock).  That reference traces back to toyotaYaris.

The getRegistrationNumber() would be a method within the Car Class and shown as an example.

I hope this was useful, please feel free to leave comments.

Leave a Reply