I have 2 different Objects(with their own methods) inside a Collection :
public class Hero {
[...]
public int getLevel(){
return level
}
}
public class Monster {
[...]
public int getLevel(){
return level
}
}
I shuffle my Collection to randomly change the order of elements :
List<Object> fighters = Arrays.asList(hero, monster);
Collections.shuffle(fighters);
After that, I need to iterate on the Collection and call Object's methods (they got the same name for both objects).
Is it possible ?
I tried with for loop, Iterator and I always receive error message :
The method getLevel() is undefined for the type Object
Example of my last tries:
1
Iterator<Object> iterator = fighters.iterator();
while (iterator.hasNext()) {
Object ob = iterator.next();
System.out.println("character level :" + ob.getLevel());
}
2
for (Object p : fighters) {
System.out.println("character level :" + p.getLevel());
;
}
Q: How can I iterate on a Collection and use Object methods?
Let me know if I can be more clear (i didn't find how to make a java snippet)