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.
答案 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();
}