When I want to cast the ArrayList
object in List
reference that returned from the method:
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
The return type from the method asList
is a List
reference to ArrayList
object, which means I can cast it to ArrayList
reference.
Such that :
String[] colors = { "black", "blue", "yellow" };
ArrayList<String> links = (ArrayList<String>) Arrays.asList(colors) ;
But this code made run-time error :
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
Why it's happened?