完整报错:
HTTP Status 500 – Internal Server Error</h1><hr class="line" /><p><b>Type</b> Exception Report</p><p><b>Message</b> Request processing failed; nested exception is java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList</p><p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.</p><p><b>Exception</b></p><pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
报错代码:
String[] temp = s.split(",");
//将结果作为ArrayList返回
return (ArrayList<String>)Arrays.asList(temp);
更正后写法:
private ArrayList<String> strToArrayList(String s){
String[] temp = s.split(",");
//将结果作为ArrayList返回
List<String> result = Arrays.asList(temp);
ArrayList<String> arrayList= new ArrayList<>(result);
return arrayList;
}
错误原因:java.util.Arrays.ArrayList下有一个ArrayList子类,所以会出现这个错误,不能直接强转类型。
asList方法返回的是一个List,所以可以通过两步来将它转换成java.util.ArrayList的类型。