-5

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.

Bob
  • 1,779
  • 3
  • 22
  • 35

1 Answers1

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();
}
Maxim Kasyanov
  • 938
  • 5
  • 14