I\'m new to Java and am trying to understand why the first code snippet doesn\'t cause this exception but the second one does. Since a string array is passed into Arrays.as
First, Arrays.asList() should be never casted to ArrayList. Second, since generics were introduced into java programming language casting is still relevant when using legacy, pre-generics APIs.
Third, never use concrete classes at the left of assignment operator.
Bottom line, say
List> stuff = new ArrayList>();
String line = "a,b,cdef,g";
String delim = ",";
String[] pieces = line.split(delim);
stuff.add(Arrays.asList(pieces));
List> stuff = new ArrayList>();
String[] titles = {"ticker", "grade", "score"};
stuff.add(Arrays.asList(titles));
and be happy.