我是靠谱客的博主 务实绿茶,这篇文章主要介绍Java中ArrayList remove会遇到的坑前言,现在分享给大家,希望可以做个参考。

前言

平时最常用的莫过于ArrayListHashMap了,面试的时候也是问答的常客。先不去管容量、负载因子什么的,就是简单的使用也会遇到坑。
array.jpg

Remove 元素

经常遇到的一个场景是:遍历list, 然后找到合适条件的给删除掉,比如删除所有的偶数。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Test public void testRemove2(){ List<Integer> integers = new ArrayList<>(5); integers.add(1); integers.add(2); integers.add(2); integers.add(4); integers.add(5); for (int i = 0; i < integers.size(); i++) { if (integers.get(i)%2==0){ integers.remove(i); } } System.out.println(integers); }

看起来好像没问题,加入面试的时候当面问:输出结果是什么?再问真不会报错吗?再问结果是什么?

  • 报错
  • 结果是空list
  • 结果是[1, 2, 5]

List.remove()有两个,一个public E remove(int index),一个是public boolean remove(Object o),那下面的结果是什么:

复制代码
1
2
3
4
5
6
7
@Test public void testRemove(){ ArrayList<Integer> integers = Lists.newArrayList(1, 2, 3, 4); System.out.println(integers); integers.remove(1); System.out.println(integers); }
  • [1, 3, 4]

经常会使用一个Arrays.asList的API, 那么下面的结果是什么:

复制代码
1
2
3
4
5
6
@Test public void testRemove3(){ List<String> list = Arrays.asList("a","b"); list.add("c"); System.out.println(list); }
  • 报错: java.lang.UnsupportedOperationException

使用foreach是否可以实现刚开始的问题

复制代码
1
2
3
4
5
6
7
8
9
10
11
@Test public void testRemove4(){ List<String> strings = new ArrayList<>(); strings.add("a"); strings.add("b"); strings.add("c"); strings.add("d"); for (String string : strings) { strings.remove(string); } }
  • 否,报错java.util.ConcurrentModificationException

为了性能问题,我们推荐把list.size的计算提取出来

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
@Test public void testRemove5(){ List<String> strings = new ArrayList<>(); strings.add("a"); strings.add("b"); strings.add("c"); strings.add("d"); int size = strings.size(); for (int i = 0; i < size; i++) { strings.remove(i); } }
  • 报错: java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
  • 这是很好的习惯, 不像开头那样每次循环都计算一次size,而且按这种情况还可以再运行的时候报错。文初的做法不报错,但结果并不是我们想要的。

使用Iterator是不是就可以remove了

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Test public void testRemove6(){ List<String> strings = new ArrayList<>(); strings.add("a"); strings.add("b"); strings.add("c"); strings.add("d"); Iterator<String> iterator = strings.iterator(); while (iterator.hasNext()){ String next = iterator.next(); strings.remove(next); } System.out.println(strings); }
  • 报错: java.util.ConcurrentModificationException

正确的remove做法是什么

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test public void testRemove7(){ List<String> strings = new ArrayList<>(); strings.add("a"); strings.add("b"); strings.add("c"); strings.add("d"); Iterator<String> iterator = strings.iterator(); while (iterator.hasNext()){ String next = iterator.next(); iterator.remove(); } System.out.println(strings); }

最后

以上就是务实绿茶最近收集整理的关于Java中ArrayList remove会遇到的坑前言的全部内容,更多相关Java中ArrayList内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部