arraylist.foreach跳过循环
时间: 2023-09-02 11:11:24 浏览: 96
在foreach循环中,当遇到null的集合时,不会自动跳过循环。实际上,如果集合为null,会抛出NullPointerException异常。在你提供的代码中,如果将final List<String> list = null;修改为final List<String> list = new ArrayList<>();,foreach循环将不会被跳过,并且不会抛出异常,因为此时list是一个空的ArrayList对象。所以,在这个情况下,foreach循环会正常执行,但是不会有任何输出。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [foreach循环报NPE空指针异常](https://blog.csdn.net/lewky_liu/article/details/83050645)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [【Array】arr.forEach()(跳出循环)](https://blog.csdn.net/wuli_youhouli/article/details/125886284)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文