Finding all instances of a given class in an array
<p>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</p>

<pre><code>Object[] arr = {"mystring", new Boolean(false), new Integer(4), new Character('i')}
</code></pre>

<p>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 <code>{"mystring"}</code>.</p>

<p>I've tried things like</p>

<pre><code>public void printInstancesOf(Class c, Object[] array)
{
    for (Object obj : array)
    {
        if (obj instanceof c)
        {
             System.out.println(obj);
        }
    }
}
</code></pre>

<p>But that doesn't even compile. Does anyone know how to do this? Or is it not possible to do?</p>
