java.lang.ClassCastException: java.util.Arrays$ArrayList 不能转换为 java.util.ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28851652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:56:39  来源:igfitidea点击:

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

javaarraylistcasting

提问by SaintLike

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 sos1.getValue();

所以我在使用 Oracle-ADF 和穿梭组件。我使用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. Therefore I've created the ArrayList sos1Value

getValue() 方法返回一个对象,我正在尝试将其转换为 ArrayList,以便以后可以使用它。因此我创建了ArrayList sos1Value

However, this line of code is going bananas:

然而,这行代码正在疯狂:

sos1Value = (ArrayList) Arrays.asList(sos1.getValue());

And I keep having a 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 tryed other ways like: sos1Value = (ArrayList) sos1.getValue();

我尝试过其他方法,例如: sos1Value = (ArrayList) sos1.getValue();

But I keep having the same problem, what can I do?

但是我总是遇到同样的问题,我该怎么办?

采纳答案by Jon Skeet

Arrays.asListreturns a Listimplementation, 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.

Arrays.asList返回一个List实现,但它不是一个java.util.ArrayList. 它的类名恰好为ArrayList,但这是一个嵌套类Arrays- 与java.util.ArrayList.

If you needa 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)。

回答by Eran

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.

Arrays.asList(sos1.getValue());生成一个 List 实现 ( java.util.Arrays$ArrayList)的实例,它不是 java.util.ArrayList。因此你不能将它投射到java.util.ArrayList.

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

如果您将 的类型更改sos1ValueList,则不需要此演员表。

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()));

回答by TheLostMind

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

ArrayList由归国Arrays.asList()方法不是java.util.ArrayList它是一个静态内部类内部Arrays类。所以,你不能把它投射到java.util.ArrayList.

Try converting / assigning it to a List.

尝试将其转换/分配给List.