JAVA-for each用法

for each 循环

  1. 这是java中功能很强的一种循环方式,可以用来依次处理数组中的每个元素,而不必定义下标值。
  2. 语法格式为:for(variable,collection) statement
    variable,定义一个变量暂存集合中的每一个元素,并执行相应语句。
    collection,这一集合必须是一个数组或者一个实现了Iterable接口的类对象。
    statement,写下语句块,实现的功能。
  3. 官方定义如下:
default void forEach​(Consumer<? super T> action)
Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.
The behavior of this method is unspecified if the action performs side-effects that modify the underlying source of elements, unless an overriding class has specified a concurrent modification policy.

Implementation Requirements:
The default implementation behaves as if:


     for (T t : this)
         action.accept(t);
 
Parameters:
action - The action to be performed for each element
Throws:
NullPointerException - if the specified action is null
Since:
1.8

  1. for each实现的功能完全能被for来替代。

  2. 附上链接: forEach 官方链接

                        希望文章对你有帮助!
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值