java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

完整报错:

HTTP Status 500 – Internal Server Error


Type Exception Report

Message Request processing failed; nested exception is java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

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)Arrays.asList(temp);

更正后写法:

 

   private ArrayList strToArrayList(String s){
        String[] temp = s.split(",");

        //将结果作为ArrayList返回
        List result = Arrays.asList(temp);
        ArrayList arrayList= new ArrayList<>(result);

        return arrayList;

    }

 错误原因:java.util.Arrays.ArrayList下有一个ArrayList子类,所以会出现这个错误,不能直接强转类型。

asList方法返回的是一个List,所以可以通过两步来将它转换成java.util.ArrayList的类型。

 

你可能感兴趣的:(JAVA)