Search code examples
javastringarraylistinstanceof

instanceof in java on a nested array list


How come i get an error at the if statement, what can i do to find the datatype the nested array list can hold?

ArrayList<List<? extends Object>> table;
table = new ArrayList<List<? extends Object>>();
table.add(new ArrayList<String>());
if( table.get(0) instanceof List<String> ){
//Do something
}

Solution

  • You cannot use instanceof with a generic type. Generic types are erased at runtime.