在foreach()中使用return

  • 效果:退出当前条件下的循环并执行下一次循环
List<Integer> testList = new LinkedList<>();
        testList.add(0);
        testList.add(1);
        testList.add(2);
        testList.add(3);

        testList.forEach(val -> {
            if (val == 2) {
                return;
            }
            System.out.println(val);
        });
  • 结果如下

Java List——foreach()中使用return/break/continue

foreach()中使用break

  • 效果:不会退出当前循环,不起作用

foreach()中使用continue

  • 效果:不会退出当前条件下的循环并执行下一次循环,不起作用

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
  • 2021-10-18
  • 2021-12-13
猜你喜欢
  • 2022-12-23
  • 2021-05-15
  • 2022-02-17
  • 2022-01-09
  • 2021-10-27
  • 2021-07-21
  • 2022-12-23
相关资源
相似解决方案