Java ,For loop command

时间:2018-06-04 17:17:30

标签: java loops for-loop

What is the meaning of this?

for(Ship s: p.ships)

Ship is the class, s is the object of the class Ship, and p is the player.

These are commands from the game Battleship.

1 个答案:

答案 0 :(得分:6)

This code means that p.ships is a some of collection implements inteface Iterable.

for(Ship s: p.ships) 

It is a foreach loop. Syntax sugar which was introduced in java 5.

This is equivalent of this statement above:

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