我試圖運行我已經在Java中做了一個程序,但我不知道發生了什麼,它 是給我下面的錯誤:的Java的ArrayList
Exception in thread "main" java.lang.ClassCastException:
java.lang.String cannot be cast to java.util.ArrayList
下面是代碼:
public static ArrayList connections(ArrayList list3) {
ArrayList connections = new ArrayList();
int row1 = 1;
int row2 = 0;
int col = 0;
connections.add(new ArrayList());
((ArrayList)connections.get(0)).add(0);
((ArrayList)connections.get(0)).add(1);
System.out.print(((ArrayList)connections.get(0)).get(0));
while(row1 < list3.size()) {
if(((ArrayList)list3.get(row1)).get(col).equals(((ArrayList)connections.get(row2)).get(col))){
connections.add(((ArrayList)list3.get(row1)).get(1));
row1++;
}
else {
connections.add(new ArrayList());
connections.add(((ArrayList)list3.get(row1)).get(0));
row2 = row1;
row1++;
}
}
return connections;
}
看來,錯誤是在if語句。有人可以幫助我嗎?
如果您查看異常的堆棧跟蹤,它應該顯示錯誤在哪一行。這有助於發現錯誤。 –
使用[泛型](http://www.oracle.com/technetwork/articles/javase/generics-136597.html)將幫助您避免這些錯誤。 –
我看到括號:O – Jacob