Java For-Each loop A delightful shortcut.

Slides:



Advertisements
Similar presentations
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
Advertisements

Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Loops For, While, and Do While. Loop structure Loops need a control variable to operate correctly. This variable must be declared, initialized, updated,
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
COMP More About Arrays Yi Hong June 05, 2015.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Basics.
9/27/2016IT 1791 Abstraction A tool (concept) to manage complexity Hide irrelevant details; focus on the features needed Primitive date types are already.
Sections 10.1 – 10.4 Introduction to Arrays
Java Arrays and ArrayLists COMP T1 #5
Lecture 23:More on Interfaces
Lecture 20: Wrapper Classes and More Loops
Java: normales Array.
Two Dimensional Array Mr. Jacobs.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Game Extras Pepper.
Generics and Subtyping
The for-each or for-all loop introduced in Java 5
Intro to Collections.
[Array, Array, Array, Array]
Arrays and the ArrayList Class The ArrayList Class
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Welcome to CSE 143!.
Array List Pepper.
"First things first, but not necessarily in that order " -Dr. Who
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
ArrayLists.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Chapter 8 Slides from GaddisText
Iterator.
Arrays versus ArrayList
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
int [] scores = new int [10];
ArrayList Collections.
ArrayLists.
Welcome to CSE 143!.
Object-Oriented Programming Paradigm
Dynamic Data Structures and Generics
Object Oriented Programming in java
Why arrays are cool 4/25/2007.
Welcome to CSE 143! Go to pollev.com/cse143.
Chapter 7 The Java Array Object © Rick Mercer.
Collections James Brucker.
[Array, Array, Array, Array]
Methods Copying Autoboxing
int [] scores = new int [10];
Arrays.
Managing Collections of Data
Lecture 13: ArrayLists AP Computer Science Principles
Code Refresher Test #1 Topics:
ArrayLists 22-Feb-19.
Interator and Iterable
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Object Oriented Programming
JavaScript: Arrays.
"First things first, but not necessarily in that order " -Dr. Who
slides created by Alyssa Harding
Java: Variables, Input and Arrays
Comparing Python and Java
Iterators Dan Fleck.
Objects with ArrayLists as Attributes
Big-O Analysis Example
ArrayLists Readings: 10.1 (pg. 559 – 571).
Arrays and ArrayLists.
Why not just use Arrays? Java ArrayLists.
Python List.
Presentation transcript:

Java For-Each loop A delightful shortcut

When should you use for-each? To access every element in a collection NOT if you plan to remove or replace anything!

General form for (SomeType var : collection){ // the variable var represents // each item in the collection one at a time } SomeType is any type of Object Collection could be an array, ArrayList, set, queue, etc.

Example 1 Given: String[] saWords = {“apple”, “banana”, “coconut”, “durian”, “fig”}; “normal” for loop for-each for(int f=0;f<saWords.length; f++) System.out.println(saWords[f]); for( fruit: saWords) System.out.println(fruit);

Example 2 Given: int[] naNums = {45,62,-7,17,23}; “normal” for loop for-each for(int nTemp:naNums) System.out.println(nTemp); for(int n=0;n<naNums.length;n++) System.out.println(naNums[n]);

Example 3 Given: ArrayList<String> aBunch = new ArrayList<String>(); aBunch.add(“dog”); aBunch.add(“cat”); aBunch.add(“bird”); aBunch.add(“rat”); for(int nI=0;nI<aBunch.size();nI++) System.out.println(aBunch.get(nI)); for(String s:aBunch) System.out.println(a);

With an ArrayList you can NOT Assign values while iterating Traverse 2 structures at once Access more than 1 element at a time It always goes from beginning to end.

Iterate the daNums array for( double d: daNums){ System.out.println(d);

Iterate the humanoidCryptids ArrayList for(String h: humanoidCryptids) System.out.println(h);