I'm still pretty new to Java, so I might be missing something obvious here.
I have the following code that I use to pick class types from a list of all my entities:
public Array<?> PickEntities(Class<?> cls) { Array<? super SpriteEntity> allEntities = new Array<Object>(); for (SpriteEntity entity : MyGame.AllEntities) { if (entity.getClass() == cls) { allEntities.add(entity); } } return allEntities; } That works fine, but it means that when calling this method I still need to cast it to the class on the other side. For example:
asteroid = (Asteroid)PickEntities(Asteroid.class); What I would like to do is use the class I am passing to my PickEntities class (the cls parameter) and then cast the returning array (allEntities) to that type.
Is there a way to do this? Whenever I try it just tells me that 'cls' is not a type and can't be used to cast.