gpt4 book ai didi

java - 在 Java 中将 For 转换为 do while

转载 作者:行者123 更新时间:2023-12-01 16:49:57 27 4
gpt4 key购买 nike

 for (String item : someList) {
System.out.println(item);
}

使用“do while”是否等效?

谢谢。

最佳答案

如果列表为空,for 循环不会创建任何输出。因此,等效的 do while 循环类似于:

    if (!someList.isEmpty()) {
Iterator<String> iterator = someList.iterator();
do {
System.out.println(iterator.next());
} while (iterator.hasNext());
}

如果在 do while 循环之前不检查空列表,则会收到 NoSuchElementException

关于java - 在 Java 中将 For 转换为 do while,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42484419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com