How can I determine the type of object an ArrayList iterator will return next?
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 ve开发者_运维技巧hicleIterator = 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
Object o = vehicleIterator.next();
if (o instanceof Car) // Is a car
if (o instanceof Bus) // ...
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."]
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.
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();
// ...
}
You can always use getClass()
in case you do not know exactly want to expected.
Generics can of great help here. You can then use instanceof
to to determine Car of Bus.
精彩评论