The for-each loop

6.8 The for-each loop

The next step we have to achieve, now that we have a list of all bodies, is to apply the gravita- tional force from each body to our movement.

We will do this by going through our list of bodies one by one, and applying the force from each body in turn.

Concept:

Java has a specialized loop for stepping through every element of a collection, and we can use

The for-each loop

this loop here. It is called a for-each loop, and it is written using the following pattern:

is another kind of loop. It is well

for ( ElementType variable : collection ) suited to process

{ all elements of a

statements ;

collection. }

92 | Chapter 6 ■ Interacting objects: Newton’s Lab

In this pattern, ElementType stands for the type of each element in the collection, variable is a variable that is being declared here, so we can give it any name we like, collection is the name of the collection we wish to process, and statements is a sequence of statements we wish to carry out. This will become clearer with an example.

Using our list named bodies , we can write

for (Body body : bodies) {

body.move(); }

(Remember that Java is case sensitive: Body with an uppercase “B” is different from body with a lowercase “b”. The uppercase name refers to the class, the lowercase name refers to a variable holding an object. The plural version— bodies —is another variable that holds the whole list.)

We can read the for-each loop a little more easily if we read the keyword for as “for each”, the colon as “in”, and the opening curly bracket as “do”. This then becomes

for each body in bodies do:...

This reading also gives us a hint as to what this loop does: It will execute the statements in the curly brackets once for each element in the list bodies . If, for example, there are three elements in that list, the statements will be executed three times. Every time, before the statements are executed, the variable body (declared in the loop header) will be assigned one of the list ele- ments. Thus, the sequence of action will be

body = first element from ‘bodies’; execute loop statements; body = second element from ‘bodies’; execute loop statements; body = third element from ‘bodies’; execute loop statements; ...

The variable body is available to be used in the loop statements to access the current element from the list. We could then, for example, call a method on that object, as in the example shown above, or pass the object on to another method for further processing.

We can now use this loop to apply gravity from all other bodies to this one:

for (Body body : bodies) {

applyGravity(body); }

In this code, we just take each element (stored in the variable body ) and pass it to another method named applyGravity , which we will have to write in a moment.

We should add one more thing: Since bodies is a list of all bodies in space, it includes the current object (the one we want to apply gravity to) as well. We do not need to apply gravity of an object to itself, so we can add an if statement that calls applyGravity only if the element from the list is not the current object itself.

6.9 Applying gravity

The result is shown in Code 6.6. Note how the keyword this is used here to refer to the current object.

Code 6.6 Applying gravity from

private void applyForces() all other bodies in

{ space

List<Body> bodies = getWorld().getObjects(Body. class ); for (Body body : bodies)

{ if (body != this ) {

applyGravity (body); } }

* Apply the gravity force of a given body to this one. */

private void applyGravity(Body other) {

// work to do here