有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的数组类型是否像“Iterable”一样具有“forEach()”方法?

foreach循环适用于Iterable接口和数组类型

它通过调用Iterable接口的forEach方法来工作

Java中的数组类型是否像Iterable一样具有forEach()方法?foreach循环是否也通过数组类型的forEach方法对数组类型起作用

我读过How does primitive array work with new for each loop in Java?,但我不确定它是否回答了我的问题


共 (2) 个答案

  1. # 1 楼答案

    Java8在Iterable接口中引入的forEach方法与您所称的“foreach循环”无关(实际名称对循环进行了增强)

    增强的for循环是在早期的Java版本中引入的,它不使用forEach()方法。它使用由实现Iterable的类实现的iterator()方法。对于数组,它在没有迭代器的情况下工作。它只使用数组的索引在元素上循环

  2. # 2 楼答案

    foreach loop works for both Iterable interface and array types.

    It works by calling the forEach method of Iterable interface.

    不,没有

    Do array types in Java have forEach() method, just as Iterable does?

    没有。请参阅here了解他们拥有的详细信息

    Does foreach loop work for array types also by forEach method of array types?

    没有

    I have read How does primitive array work with new for each loop in Java?, but I am not sure if it answers my question.

    是的