Presentation is loading. Please wait.

Presentation is loading. Please wait.

The for-each or for-all loop introduced in Java 5

Similar presentations


Presentation on theme: "The for-each or for-all loop introduced in Java 5"— Presentation transcript:

1 The for-each or for-all loop introduced in Java 5
The New For Loop The for-each or for-all loop introduced in Java 5 Copyright © Curt Hill

2 The Old For The for loop is essentially the same as that of C and C++
Not changed since early 1970s It is a counting loop, where you show three things: Where to start counting Where to end counting What to count by Sometimes that is not so easy Copyright © Curt Hill

3 Collections There are a number of collections where it is not so easy to infer These include: pixels in a picture items in an array items in one of many Java data structures The new for is designed to access each one without having to know how many there are Copyright © Curt Hill

4 The New For The new for infers those three things
It must infer them given a situation In the next screen we will consider how to do this with a counting for Copyright © Curt Hill

5 Old picture processing
Consider one way to do this: static void process (Picture p){ Pixel [] px = p.getPixels(); int len = px.length; for(int i = 0;i<len;i++){ Pixel ap = px[i]; ap.setRed(0); } // end of for } // end of method Copyright © Curt Hill

6 New picture processing
Now we do it with the new for Picture p … ; Pixel [] px = p.getPixels(); for(Pixel ap: px){ // process ap } The variable ap represents the pixel and may be processed This variable has scope only in the for loop Copyright © Curt Hill

7 Inferring The Java compiler examines the array for the questions of the old for Where to start: Arrays always start at zero Where to end: Arrays contain a length What to count by: Make sure each one is touched Copyright © Curt Hill

8 The Form of the For The form is different: for(type var : object)
Where type is the type of array object var is the name of the one item that the body will work on object is the original array name Variable var will receive the values from the array Notice that there is nothing like a subscript or subscripting Copyright © Curt Hill

9 Picture processing again
The new for does nothing that the old did not do Picture p … ; Pixel [] px = p.getPixels(); for(Pixel ap: px){ // process ap } Now we infer the start and end subscripts The subscripting is hidden from us Copyright © Curt Hill

10 Iteration This for is actually an iterator
It will walk through any kind of data structure and touch each item Even if that data structure is not linear Arrays are linear Trees are not Fortunately, we do not have to worry about this during this semester Copyright © Curt Hill

11 In Conclusion For picture processing we can use either
Sometimes that may not be the case Counting for explicitly shows the initialization, test and advancement For all infers them all May only be used with container objects The only container we will see this semester is the array, but others exist Copyright © Curt Hill


Download ppt "The for-each or for-all loop introduced in Java 5"

Similar presentations


Ads by Google