The other form of for loop

Download Report

Transcript The other form of for loop

The New For Loop
The for-each or for-all loop
introduced in Java 5
Copyright © 2005-2010 Curt Hill
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 © 2005-2010 Curt Hill
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 © 2005-2010 Curt Hill
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 © 2005-2010 Curt Hill
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 © 2005-2010 Curt Hill
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 © 2005-2010 Curt Hill
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 © 2005-2010 Curt Hill
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
© 2005-2010 Curt Hill
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 © 2005-2010 Curt Hill
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 © 2005-2010 Curt Hill
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 © 2005-2014 Curt Hill