I have a program where I need to find all instances of a class within an array. I would like to create a method that does this for me. For instance, if I have the array
Object[] arr = {"mystring", new Boolean(false), new Integer(4), new Character('i')}
and I called this method with argument of String (I'm not sure what the type of my argument should be, actually), it would return an array of {"mystring"}
.
I've tried things like
public void printInstancesOf(Class c, Object[] array)
{
for (Object obj : array)
{
if (obj instanceof c)
{
System.out.println(obj);
}
}
}
But that doesn't even compile. Does anyone know how to do this? Or is it not possible to do?