我想清除try-catch中的ArrayList。我知道我們可以用clear()
方法來做到這一點。但是當我嘗試用for循環清除每個項目時。它顯示如下結果:用try-catch Java ArrayList問題
public class ArrayListTryCatch {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList();
int oldsize = list.size();
try {
list.add(20);
list.add(30);
list.add(40);
int wrongDivide = 1/0;
} catch (Exception e) {
for (int i = 0; i < list.size(); i++) {
System.out.println("deleted item: " + list.get(oldsize));
list.remove(i);
}
}
System.out.println(list.size());
System.out.println("after try catch: ");
for (Integer e : list) {
System.out.println(e);
}
}
}
,其結果是:
deleted item: 20
deleted item: 30
1
after try catch:
30
我鑫卡特它必須是空的ArrayList。我有什麼麻煩?
第一種方式應該是我 - – haind
@haind:Doh--修好了,謝謝。 –