java集合框架list的remove方法

    list.add(1);
    list.add(2);
    list.add(3);
    list.remove(1);
    如果我想删除的是 1 这个元素,可是正好把1当做角标把 2 元素删除了,请问应该怎么删除 1 这个元素?

可以把你要删除的数字先装箱,比如要删除1,就Integer i = 1; list.remove(i);

 list.remove(0);
/**
 * Removes the element at the specified position in this list.
 * Shifts any subsequent elements to the left (subtracts one from their
 * indices).
 * 所有元素都往左移动(下标都减1)
 */

**

这就意味着,移除掉下标为1的元素,只会让下标>1的元素下标集体左移。也就是说1之前的下标元素都不会变。**

remove的参数是index,既索引,而不是元素内容
所以就remove(0).....

集合,数组都是从零开始的,所以下标为零的就是你要删除的数据

数组、集合的索引都是从0开始的,所以,你要删除第一个元素,是list.remove(0);

集合使用迭代器 别使用for循环之类的

for(int i=0;i if ("ab".equals(list.get(i))){
list.remove(i);
--i;
}
}
Iterator it = list.iterator();
while (it.hasNext()){
String str = it.next();
if ("ab".equals(str)){
it.remove();
}
}