[英]java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
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.所以我在使用 Oracle-ADF 和穿梭组件。 I get the selected values using the
sos1.getValue();
我使用
sos1.getValue();
获取选定的值sos1.getValue();
The getValue() method returns an object and I'm trying to convert it to an ArrayList so I can work with it later. getValue() 方法返回一个对象,我正在尝试将其转换为 ArrayList,以便以后可以使用它。 Therefore I've created the
ArrayList sos1Value
因此我创建了
ArrayList sos1Value
However, this line of code is going bananas:然而,这行代码正在疯狂:
sos1Value = (ArrayList) Arrays.asList(sos1.getValue());
And I keep getting java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
我不断收到
java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
I've tried other ways like: sos1Value = (ArrayList) sos1.getValue();
我尝试过其他方法,例如:
sos1Value = (ArrayList) sos1.getValue();
But I keep having the same problem, what can I do?但是我总是遇到同样的问题,我该怎么办?
Arrays.asList
returns a List
implementation, but it's not a java.util.ArrayList
. Arrays.asList
返回一个List
实现,但它不是一个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
.它碰巧有一个
ArrayList
的类名,但它是Arrays
中的一个嵌套类 - 与java.util.ArrayList
完全不同的类型。
If you need a java.util.ArrayList
, you can just create a copy:如果你需要一个
java.util.ArrayList
,你可以创建一个副本:
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
). (如果您不需要任何仅由
ArrayList
公开的成员)。
Arrays.asList(sos1.getValue());
produces an instance of a List implementation ( java.util.Arrays$ArrayList
) that is not java.util.ArrayList.生成一个 List 实现(
java.util.Arrays$ArrayList
)的实例,它不是 java.util.ArrayList。 Therefore you can't cast it to java.util.ArrayList
.因此你不能将它转换为
java.util.ArrayList
。
If you change the type of sos1Value
to List
, you won't need this cast.如果将
sos1Value
的类型sos1Value
为List
,则不需要此演员表。
If you must have an instance of java.util.ArrayList
, you can create it yourself :如果你必须有一个
java.util.ArrayList
的实例,你可以自己创建它:
sos1Value = new ArrayList (Arrays.asList(sos1.getValue()));
The ArrayList
returned by Arrays.asList()
method is NOT java.util.ArrayList
it is a static inner class inside Arrays
class. Arrays.asList()
方法返回的ArrayList
不是java.util.ArrayList
它是Arrays
类中的静态内部类。 So, you can't cast it to java.util.ArrayList
.所以,你不能将它转换为
java.util.ArrayList
。
Try converting / assigning it to a List
.尝试将其转换/分配给
List
。
The easy way (not efficient) is简单的方法(效率不高)是
ArrayList sos2Value = new ArrayList()
sos2Value.addAll(Arrays.asList(sos1.getValue()))
sos2Value is java.util.ArrayList. sos2Value 是 java.util.ArrayList。
Arrays.asList(sos1.getValue()) is java.util.Arrays$ArrayList Arrays.asList(sos1.getValue()) 是 java.util.Arrays$ArrayList
But I'm not clear what you want to do.但我不清楚你想做什么。
java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList java.lang.ClassCastException: java.util.Arrays$ArrayList 不能转换为 java.util.ArrayList
You need to set the type to java.util.List您需要将类型设置为 java.util.List
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.