昨天一位项目组同事需要在ArrayList中删除所有不等于指定值的元素,但是她弄了很久,发现总是删得不完全。刚好我以前做过类似的功能,就告诉她是因为ArrayList删除元素后长度变小了,元素的索引也会跟着改变,但是迭代的下标没有跟着相应的改变的缘故。
将一些删除方法做一些总结:
/**
* 删除Arraylist中值为"c"的元素
*/
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
//"c"在Arraylist不连续存储
/*
list.add("c");
list.add("a");
list.add("c");
list.add("b");
list.add("c");
list.add("d");
list.add("c");
*/
//"c"在Arraylist有连续存储
list.add("a");
list.add("c");
list.add("c");
list.add("b");
list.add("c");
list.add("c");
list.add("d");
list.add("c");
//删除Arraylist中值为"c"的元素
//有可能不能全部删除
//removeListElement1(list);
//能够正确删除
//removeListElement2(list);
//能够正确删除
//removeListElement3(list);
}
/**
* 删除list中值为"c"的元素
*
* 这种方式:
*
* 当值为"c"的元素在Arraylist中不连续存储的时候,是可以把值为"c"的元素全部删掉
*
* 但是当值为"c"的元素在Arraylist中有连续存储的时候,就没有把值为"c"的元素全部删除
* 因为删除了元素,Arraylist的长度变小了,索引也会改变,但是迭代的下标没有跟着变小
*/
public static void removeListElement1(List<String> list) {
for(int i=0;i<list.size();i++) {
if("c".equals(list.get(i))) {
list.remove(i);
}
}
}
/**
* 删除Arraylist中值为"c"的元素
*
* 这种方式:
*
* 不管值为"c"的元素在Arraylist中是否连续,都可以把值为"c"的元素全部删除
*/
public static void removeListElement2(List<String> list) {
for(int i=0;i<list.size();i++) {
if("c".equals(list.get(i))) {
list.remove(i);
--i;//删除了元素,迭代的下标也跟着改变
}
}
}
/**
* 删除Arraylist中值为"c"的元素
*
* 这种方式:
*
* 不管值为"c"的元素在list中是否连续,都可以把值为"c"的元素全部删除
*
* 需保证没有其他线程同时在修改
*/
public static void removeListElement3(List<String> list) {
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()) {
String str = iterator.next();
if("c".equals(str)) {
iterator.remove();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
原文链接:http://www.cnblogs.com/luxh/archive/2012/05/19/2509314.html