I have an abstract class called object. I have an arraylist called objs. I want to pull out only a class that extends object called enemy. Do I use getClass().getSimpleName()? It does not work. What do I do? Make another arraylist all together?
I dont really get what you mean, maybe this?
object getObject(ArrayList list){
for (int i = 0; i < list.lenght(); i++)
if (list.get(i) instanceof object)
return ((object)list).get(i);
}
^
^ yup, that.
Read this: isAssignableFrom() vs. instanceof
So you can either do what you want using isAssignableFrom() or instanceOf.
You’d actually want to do
list.get(i) instanceof Enemy
Considering everything in the list is an Object. And yes, you’d ultimately have to cast it as an Enemy.
Using casts like that can be sign of code smell. See here:
http://alvinalexander.com/java/java-command-design-pattern-in-java-examples