-1

In advance I apologize for any mistakes in terminology, it's been a while since I've used java So I have the constructor

Planet[] stars = new Planet [500]

The aim is to call a draw() command on each object in stars. Previously I used the loop

for (int i = 0; i < 500;, i++)
 stars[i].draw(ss);

Although I have recently learnt of the foreach statement, if that is what the colon is called. So instead I try

for (int i : stars)
 stars[i].draw(ss);

Although upon compiling I receive the error "incompatible types: Planet cannot be converted to int"

I have tried typecasting but it will not allow that either.

Thanks for any help offered

  • I would have duplicated to http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work esp. the second answer. – GhostCat Oct 20 '16 at 19:33

1 Answers1

0

Try this:

for (Planet planet : stars) {
   planet.draw(ss);
}
Daniel A. Thompson
  • 1,904
  • 1
  • 17
  • 26