chichung
public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

        for (String str:list) {
            System.out.println(str);
        }
    }

 

需要注意的是,增强for循环底层使用迭代器实现的,所以不能在遍历的时候修改集合(与迭代器同理),否则会出现并发修改异常。

如果有修改集合的需求,可以用for循环,或者特殊的迭代器的添加方法(例如ListIterator的add方法)。

分类:

技术点:

相关文章:

  • 2021-04-13
  • 2021-10-04
  • 2021-10-04
  • 2021-10-04
  • 2022-02-11
  • 2021-10-03
  • 2021-10-03
猜你喜欢
  • 2021-10-04
  • 2021-10-14
  • 2021-10-04
  • 2021-10-04
  • 2021-10-04
  • 2021-10-04
  • 2021-10-04
相关资源
相似解决方案