java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

前端 未结 3 1767
时光说笑
时光说笑 2020-12-08 18:23

Can you explain me why does this happen and how can I fix it please?

So I\'m using Oracle-ADF and I\'m using shuttle components. I get the selected values using the

相关标签:
3条回答
  • 2020-12-08 18:52

    Arrays.asList returns a List implementation, but it's not a java.util.ArrayList. It happens to have a classname of ArrayList, but that's a nested class within Arrays - a completely different type from java.util.ArrayList.

    If you need a java.util.ArrayList, you can just create a copy:

    ArrayList<Foo> list = new ArrayList<>(Arrays.asList(sos1.getValue()); 
    

    or:

    List<Foo> list = new ArrayList<>(Arrays.asList(sos1.getValue())); 
    

    (if you don't need any members exposed just by ArrayList).

    0 讨论(0)
  • 2020-12-08 18:56

    The ArrayList returned by Arrays.asList() method is NOT java.util.ArrayList it is a static inner class inside Arrays class. So, you can't cast it to java.util.ArrayList.

    Try converting / assigning it to a List.

    0 讨论(0)
  • 2020-12-08 18:59

    Arrays.asList(sos1.getValue()); produces an instance of a List implementation (java.util.Arrays$ArrayList) that is not java.util.ArrayList. Therefore you can't cast it to java.util.ArrayList.

    If you change the type of sos1Value to List, you won't need this cast.

    If you must have an instance of java.util.ArrayList, you can create it yourself :

    sos1Value = new ArrayList (Arrays.asList(sos1.getValue()));
    
    0 讨论(0)
提交回复
热议问题