import java.util.ArrayList;
/**
*
* @author zhang
*
*/
public class Test {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(5);
list.add(6);
System.out.println(list);
// 删除值为2的项
// list.remove((Integer)2);//调用 public boolean remove(Object o) 方法
// 删除索引为2的元素,索引从0开始
list.remove(2);// 调用 public E remove(int index) 方法
System.out.println(list);
}
}
运行结果:
[2, 1, 5, 6]
[2, 1, 6]
使用 list.remove((Integer)2);后,运行结果:
[2, 1, 5, 6]
[1, 5, 6]