Reputation: 2301
ListIterator litr = PuntenLijst.listIterator();
while(litr.hasNext()){
Object Punt = litr.next();
Punt.print();
}
PuntenLijst is an ArrayList that contains object instances from another Class.
Now I have made a method print() that prints out something from that object (of the other class).
With this loop I try to loop through the ArrayList, and then use the print() method from the other class, but it doesn't not seem to be working.
Can anybody help me out?
Upvotes: 0
Views: 148
Reputation: 213253
Object Punt = litr.next();
Punt.print();
You need to cast your Punt
to appropriate class, of which you have the ArrayList.
Of course print()
is not a method of Object
class
Suppose you have ArrayList like this: -
ArrayList<YourClass> PuntenLijst
Change your invocation to: -
Object Punt = litr.next();
((YourClass)Punt).print();
Or: -
YourClass Punt = litr.next();
Punt.print();
Upvotes: 3
Reputation: 220
Why dont you just use for:each loop? The casting is taken care by the JVM provided you've defined PuntenLijst with the correct Type.
Upvotes: 0
Reputation: 5661
Cast Punt
to the type that contains the Print()
method.
while(litr.hasNext()){
TYPE_THAT_HAS_PRINT Punt = (TYPE_THAT_HAS_PRINT) litr.next();
Punt.print();
}
Upvotes: 2
Reputation: 66637
Cast the object you get as response for litr.next()
to corresponding type and call method on that type.
Example:
Punt puntObj =(Punt) litr.next();
puntObj.print();
Upvotes: 2