【问题标题】:Java ,For loop commandJava ,For 循环命令
【发布时间】:2018-06-04 17:17:29
【问题描述】:

这是什么意思?

for(Ship s: p.ships)

船是阶级, s 是 Ship 类的对象, p 是玩家。

这些是来自游戏战舰的命令。

【问题讨论】:

标签: java loops for-loop


【解决方案1】:

这段代码表示p.ships是一些集合实现接口Iterable

for(Ship s: p.ships) 

这是一个 foreach 循环。 java 5 中引入的语法糖。

这相当于上面的语句:

for (Iterator<Ship > i = p.ships.iterator(); i.hasNext();) {
    Ship s= i.next();
}

【讨论】: