如何修复:java.lang.ClassCastException:java.util.ArrayList 无法转换为 java.lang.Integer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25255646/
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
How to fix: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Integer
提问by Neha
I encountered a problem when I tried to run the following code:
我在尝试运行以下代码时遇到了问题:
ArrayList paretoSet=new ArrayList(); // contains a collection of ArrayList
ArrayList<Integer> toPass=new ArrayList<Integer>();
int[] fParetoSet=new int[ParetoSet.size()];
int[] gParetoSet=new int[ParetoSet.size()];
for (int i=0;i<paretoSet.size();i++){
toPass.clear();
toPass.add((Integer)paretoSet.get(i));
int [] totake=calculate(toPass);
fParetoSet[i]=totake[0];
gParetoSet[i]=totake[1];
}
`
where claculate(ArrayList x) is a method that takes an integer arraylist and returns an integer array. I can not make Paretoset an integer arraylist as it creates problem in other parts of my program. I encountered an exception in the line toPass.add((Integer)paretoSet.get(i));
as java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.IntegerHow should I fix this problem?
Thanks in advance
` 其中 claculate(ArrayList x) 是一种采用整数数组列表并返回整数数组的方法。我无法将 Paretoset 设为整数数组列表,因为它会在我程序的其他部分产生问题。我在toPass.add((Integer)paretoSet.get(i));行中遇到了异常 。
作为java.lang.ClassCastException: java.util.ArrayList 不能转换为 java.lang.Integer我应该如何解决这个问题?
提前致谢
采纳答案by Romski
If ParetoSet
is a collection of ArrayList
, then the call ParetoSet.get(i)
will return the ArrayList
at index i
. As the error says, an ArrayList
is not a a type of Integer
and cannot be cast to one.
如果ParetoSet
是 的集合ArrayList
,则调用ParetoSet.get(i)
将返回ArrayList
at 索引i
。正如错误所说, anArrayList
不是 aa 类型,Integer
不能强制转换为一个。
Other points of interest:
其他兴趣点:
- your variable should be camel case:
pareToSet
- auto-boxing means the cast in your for loop has been unnecessary since JDK5
Paretoset
has been declared with a raw type- type inference makes
new ArrayList<Integer>()
redundant since JDK7
- 你的变量应该是驼峰式:
pareToSet
- 自动装箱意味着从 JDK5 开始就不需要 for 循环中的强制转换
Paretoset
已用原始类型声明new ArrayList<Integer>()
自 JDK7 以来,类型推断变得多余
EDIT
编辑
- your variable should be camel case:
paretoSet
, as per Jim's comments
- 您的变量应该是驼峰式大小写:
paretoSet
,根据吉姆的评论
EDIT
编辑
An ArrayList is neither conceptually, or actually an Integer. If you say the sentence 'A list is a type of integer' it doesn't make sense. If we check the javadocfor ArrayList we can see that its inheritance tree is:
ArrayList 既不是概念上的,也不是实际的整数。如果你说“A list is a type of integer”这句话是没有意义的。如果我们检查ArrayList的javadoc,我们可以看到它的继承树是:
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
So we can say for example that ArrayList
is a type of AbstractList
, or AbstractCollection
, but not an Integer
as Integer
is not part of its lineage.
因此,例如,我们可以说这ArrayList
是AbstractList
, or 的一种类型AbstractCollection
,但不是Integer
asInteger
不是其谱系的一部分。
回答by Will Mcavoy
your program having typecasting issue on below lines. your are trying to add typecast list of arraylist to Integer arryalist. this wrong. if you want add the arraylist objects to some other arryalist. Please use generics as object.
您的程序在以下几行存在类型转换问题。您正在尝试将 arraylist 的类型转换列表添加到 Integer arryalist。这不对。如果您想将 arraylist 对象添加到其他一些 arryalist。请使用泛型作为对象。
ArrayList ParetoSet=new ArrayList(); // contains a collection of ArrayList
toPass.add((Integer)ParetoSet.get(i));
This should be like below:
这应该如下所示:
ArrayList<ArrayList> ParetoSet=new ArrayList<ArrayList>();
ArrayList<ArrayList> toPass=new ArrayList<ArrayList>();
toPass.add(ParetoSet.get(i));
Then you should change code bit to fit your further your logics
然后你应该改变代码位以适应你的进一步逻辑