How can I determine the type of object an ArrayList iterator will return next?

arraylist, iterator, java

Solution

Object o = vehicleIterator.next();
if (o instanceof Car) // Is a car
if (o instanceof Bus) // ...

Problem

I have an `ArrayList` and I am using an iterator to run through it. I need to find out what type of object is next: ``` Iterator vehicleIterator = vehicleArrayList.iterator(); while(vehicleIterator.hasNext()) { //How do I find the type of object in the arraylist at this point // for example, is it a car, bus etc... } ``` Thanks

Original source