Possible to create a array of referencetype?

For example I have a arraylist of objects and I would like to test what kind of object it is.

eg.

ArrayList<Object> list = new ArrayList<Object>();
list.add("StringType");
list.add(5);
list.add(new randomClass());

ArrayList<Class?> define = new ArrayList<Class?>(); // <-- part i dont know what to do
define.add(String);
define.add(int);
define.add(randomClass);

for(x=0; .. ; ..){
if(list.get(x) instanceof define.get(x))
    //do stuff 
}
//Get results if list matches define?

Or do I have to store the reference type as String and do a list.get(x).getClass().toString().equals(define.get(x));

Jon Skeet
people
quotationmark

You can use Class<?> as the type argument for the second list - and then use Class.isInstance to perform the check for "instancehood":

List<Class<?>> classes = new ArrayList<>();
classes.add(String.class);
classes.add(int.class);
classes.add(RandomClass.class);

for (int i = 0; i < list.size(); i++) {
    if (classes.get(i).isInstance(list.get(i))) {
        ...
    }
}

people

See more on this question at Stackoverflow