[英]ClassCast Exception
I've written a method that has some casting errors:我写了一个有一些铸造错误的方法:
public class Factor {
public static int[] findFactors(ArrayList<Integer> nums){
ArrayList<Integer> factors= new ArrayList();
for(Integer i=new Integer(0);i<nums.size();i++) {
System.out.println(nums.get(i));
for(int j=0;j<nums.get(i);j++) {
if (nums.get(i) %j==0) {
factors.add(j);
}
}
}
int ct=0;
String factorString= factors.toString();
char[] charArray= factorString.toCharArray();
int[] factorArray= new int[(charArray.length+1)/2];
for(int a=0;a<charArray.length;a++) {
if(charArray[a]==',') {
continue;
} else {
String s= Character.toString(charArray[a]);
factorArray[ct]=Integer.parseInt(s);
ct++;
}
}
return factorArray;
}
}
any help would be appreciated任何帮助,将不胜感激
The problem you have that you only escape the ,
but when you use toString()
on a List it is enclose in square brackets []
.您遇到的问题是,您只能转义,
但是当您在 List 上使用toString()
,它被括在方括号[]
。 When you fix that you will discover that size of result array is not valid also you have to subtract 2 (the brackets).当您修复它时,您会发现结果数组的大小无效,您还必须减去 2(括号)。
Good Luck with rest.祝你好运休息。 And please read some basic manual about Java.请阅读一些关于 Java 的基本手册。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.