我脑子里有一个问题,为什么我不能通过使用正常的For Loop从Set中获取元素,但是当我使用Enhanced For Loop时,所有事物都可以正常工作。示例:
private static Set<HeavenlyBody> planets = new HashSet<>();
for (HeavenlyBody getPlanet : planets){
System.out.println("\t"+getPlanet.getName()+": "+getPlanet.getOrbitalPeriod());
}
如果Set没有该技术,Enhanced For Loop中的getPplanet对象如何获取元素?以及如何通过常规For Loop做到这一点?最好的问候
简单的答案是:Set实现了Iterable,这就是为什么可以使用for循环通过元素进行迭代的原因
NORMAL for循环不起作用,因为Set没有get()方法。而它被省略的原因,是在数据结构上建立起来的]