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

Slides:



Advertisements
Similar presentations
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Advertisements

Copyright © Curt Hill Multiple Dimension Arrays Extending Java Arrays.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Copyright Curt Hill Arrays in C/C++ More on usage.
Animation in Minecraft Movement within Movement Copyright © 2015 – Curt Hill.
Copyright © Curt Hill Further Picture Manipulation Considering position.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Comp1004: Loops and Arrays I Whiles, For and Arrays[]
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Copyright © 2004 – 2006 – Curt Hill Linked Lists Very Powerful Data Structure.
The for Statement A most versatile loop
Recursion.
Sophomore Scholars Java
The Second C++ Program Variables, Types, I/O Animation!
Sections 10.1 – 10.4 Introduction to Arrays
Getting and displaying
Flow of Control An Overview
Chapter 4 Repetition Statements (loops)
Arrays Low level collections.
CSE 8A Lecture 17 Reading for next class: None (interm exam 4)
Very Powerful Data Structure
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Inheritance and Polymorphism
A Very Common Series of Techniques
Chapter 5: Control Structures II
Loop Structures.
Manipulating Pictures, Arrays, and Loops part 2
Document Object Model That’s DOM to you
Methods Additional Topics
Repeating Instructions And Advance collection
A brief look at some of the new features introduced to the language
Multiple Dimension Arrays
The C++ IF Statement Part 2 Copyright © Curt Hill
Doing things more than once
Concepts From Alice Switching to Java Copyright © Curt Hill.
Introduction to Processing Digital Sounds part 2
Lists in Python.
A Kind of Binary Tree Usually Stored in an Array
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
The most important decision statement
Arrays in Java What, why and how Copyright Curt Hill.
Java For-Each loop A delightful shortcut.
CS150 Introduction to Computer Science 1
Compound Statements A Quick Overview
Sridhar Narayan Java Basics Sridhar Narayan
Module 4 Loops.
Other displays Saving Arrays Using fors to process
JavaScript onLoad arrays
Methods Again Parameter Passage
ArrayLists 22-Feb-19.
Chapter 7: User-Defined Functions II
Object Oriented Programming
Arrays in Java.
February , 2009 CSE 113 B.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
And now for something completely different . . .
CSC1401 Manipulating Pictures 2
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Skip Lists The non-list List Copyright © 2016 Curt Hill.
The beginning of media computation Followed by a demo
Methods Scope How are names handled?
Week 7 - Monday CS 121.
Methods Coding in Java Copyright © Curt Hill.
While and Do While Syntax, semantics and examples
Presentation transcript:

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 © 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