views:

264

answers:

6

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

+5  A: 
Object o = vehicleIterator.next();
if (o instanceof Car) // Is a car
if (o instanceof Bus) // ...
Sinuhe
cool thanks mate
Dan
A: 

You can always use getClass() in case you do not know exactly want to expected.

NawaMan
+4  A: 

One way is to use the getClass() method.

Object obj = vehicleIterator.next();
Class type = obj.getClass();  
System.out.println("The type is: " + type.getName());

However, if you're explicitly checking class type there's almost always a better way to write your code using polymorphism or some other OO principle. Code that checks type like this, or using instanceof, would certainly have to be changed if additional types of cars are added.

Without having more information about what you're doing with the type, I'd suggest you have a base type Vehicle that Car, Bus, etc. all inherit from. Give your vehicles the methods they need (override the ones you need to) then just call those methods in your loop.

Bill the Lizard
+1 for note about **instanceof**
dfa
+7  A: 

Firstly, get some generics in there. They have been in Java since 2004. Even the version of Java SE that introduced them has completed its End of Service Life period.

(As @finnw points out, I was forgetting about poor old Java ME. If you have to use Java ME then you will need to eschew generics and cast (but not instanceof often) until it (including deployed devices) makes it into 2004.)

Using instanceof and casting tends to indicate poor design. It would probably better to place into the list an object with an interface the client code can use without tests, and implementations that map to different behaviour for each "real" target of the list. "Every problem in computer science can be solved by adding another level of indirection" ["... except too many levels of indirection."]

Tom Hawtin - tackline
+1 for "poor design" - indeed.
duffymo
I don't know about this question, but sometimes generics are omitted from Java questions because they are targeting j2me (but the poster forgot to tag it as such) or in homework problems (because the college avoids generics to simplify the language for teaching purposes.)
finnw
Another possibility is that he is using a third-party library that has not been updated with generics. Almost all Java developers know how to create generic collections by now, but not everyone knows about using wildcards (e.g. `Iterator<?> myIterator = myLegacyCollection.iterator()`) to interface with legacy code.
finnw
@finnw I did forget about poor old Java ME. (In any case you still don't want to be using `instanceof` very often.)
Tom Hawtin - tackline
+1  A: 

Since you want the class of the next element rather than the current one, consider using a peeking iterator implementation.

Then you can use a loop condition like:

while (vehicleIterator.hasNext() &&
       ForkLiftTruck.class.isAssignableFrom(vehicleIterator.peek().getClass())) {
    vehicle forkLiftTruck = (ForkLiftTruck) vehicleIterator.next();
    // ...
}
finnw
+1 for reading the question
omerkudat
A: 

Generics can of great help here. You can then use instanceof to to determine Car of Bus.

fastcodejava