Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Introduction to Java 2 Programming Lecture 5 The Collections API.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Problem Solving # 9: ArrayList, Collections and More ICS
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Grouping Objects 3 Iterators, collections and the while loop.
CS 307 Fundamentals of Computer Science 1 Lists and ArrayList many slides taken from Mike Scott, UT Austin.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
1 Topic 8 Iterators "First things first, but not necessarily in that order " -Dr. Who.
Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
CS 307 Fundamentals of Computer ScienceIterators 1 Topic 14 Iterators "First things first, but not necessarily in that order " -Dr. Who.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
Arrays And ArrayLists - S. Kelly-Bootle
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
ArrayLists, LinkedLists, Collections Section 12.1.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Animals. I live ______ Farm Wild Water Zoo Animal Picture Pairs.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
ArrayLists, LinkedLists, Collections Section 12.1.
CS2852 Week 3, Class 2 Today Big-O runtime analysis Linked Lists Muddiest Point Lab Quiz Includes writing a method from ArrayList class (See next slide)
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Java Collection Classes Com379PT
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
EKT472: Object Oriented Programming
Objects First with Java CITS1001 week 4
Java Arrays and ArrayLists COMP T1 #5
"First things first, but not necessarily in that order " -Dr. Who
Iterator.
And the list goes on and on and on….
"First things first, but not necessarily in that order." -Dr. Who
Java For-Each loop A delightful shortcut.
Collections and iterators
"First things first, but not necessarily in that order " -Dr. Who
Manu Kumar CS193J: Programming in Java Summer Quarter 2003 Lecture 3 Collections and More OOP Manu Kumar Thursday,
"First things first, but not necessarily in that order " -Dr. Who
slides created by Alyssa Harding
Iterators Dan Fleck.
Collections and iterators
Tuple.
Introduction to Java Collection
Presentation transcript:

Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal // do something with animal

Iterators List zoo = new ArrayList (); // Another way to loop over a collection Iterator it; it = zoo.iterator(); Animal animal; while (it.hasNext()) {animal = (Animal) it.next(); // Cast // next() returns a ref to Object // do something with animal // do something with animal it.remove();// remove from the collection }

Generic Classes and Iterators List zoo = new ArrayList (); Animal animal; // … add several Animals to the zoo Iterator it = zoo.iterator(); while (it.hasNext()) {animal = it.next();// No cast needed! {animal = it.next();// No cast needed! // do something with animal } // Iterator must be used when removing items from a collection.