Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3
对于整数类型,这种情况下默认为remove(int index)
方法(也就是索引列表下标
进行删除)。
public static void main(String[] args) {
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(3);
num.add(0);
num.add(4);
num.remove(3);//报错,系统默认3为下标,并不是值。此时下标仅有0,1,2
for(Integer a:num) {
System.out.print(a);
}
}
要想正确使用remove(Object o)
方法(也就是根据列表的值
进行删除)
public static void main(String[] args) {
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(3);
num.add(0);
num.add(4);
num.remove(Integer.valueOf(3));//此时3是值
for(Integer a:num) {
System.out.print(a);
}
}