Java中的数组类型是否像“Iterable”一样具有“forEach()”方法?
foreach循环适用于Iterable
接口和数组类型
它通过调用Iterable
接口的forEach
方法来工作
Java中的数组类型是否像Iterable
一样具有forEach()
方法?foreach循环是否也通过数组类型的forEach
方法对数组类型起作用
我读过How does primitive array work with new for each loop in Java?,但我不确定它是否回答了我的问题
# 1 楼答案
Java8在
Iterable
接口中引入的forEach
方法与您所称的“foreach循环”无关(实际名称对循环进行了增强)增强的for循环是在早期的Java版本中引入的,它不使用
forEach()
方法。它使用由实现Iterable
的类实现的iterator()
方法。对于数组,它在没有迭代器的情况下工作。它只使用数组的索引在元素上循环# 2 楼答案
对
不,没有
没有。请参阅here了解他们拥有的详细信息
没有
是的