Today’s Agenda  Generic  Iterators CS2336: Computer Science II.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Java Review Interface, Casting, Generics, Iterator.
Computer Science 209 Software Development Iterators.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Topic 11 Linked Lists -Joel Spolsky
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
11-Jun-15 Generics. A generic is a method that is recompiled with different types as the need arises The bad news: Instead of saying: List words = new.
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.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
©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.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
Iterators CS 367 – Introduction to Data Structures.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Lists Based on content from: Java Foundations, 3rd Edition.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
Effective Java: Generics Last Updated: Spring 2009.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
LinkedList Many slides from Horstmann modified by Dr V.
Scott Grissom, copyright 2004 Ch 6 Data Structures Slide 1 Linear Data Structures Chapter 6 focuses on: Lists Stacks Queues You can skip the Array implementation.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
1 Iterators "First things first, but not necessarily in that order " -Dr. Who CS Computer Science II.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
COMP 121 Week 11: Linked Lists.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Computer Science 209 Software Development Inheritance and Composition.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Java Collection Classes Com379PT
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Objectives After studying this chapter you should understand the following: the role of iterators in container classes; various implementations of the.
Software Development Iterators
Top Ten Words that Almost Rhyme with “Peas”
Introduction to Data Structures
Lecture 26: Advanced List Implementation
CS2013 Lecture 4 John Hurley Cal State LA.
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 21 Advanced List Implementation
Data Structures and Algorithms 2/2561
Java Generics & Iterators
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Today’s Agenda  Generic  Iterators CS2336: Computer Science II

Parameterized Stack  We implemented stack of Strings  How about the stack of other type of objects!  Do we need to implement a separate stack for each type?  Rewriting code is error-prone  Maintaining cut-and-paste code is error-prone  How about having a stackOfObjects and use casting  Run-time error if the types mismatch CS2336: Computer Science II StackOfObj s = new StackOfObj(); Apple a = new Apple(); Orange o = new Orange(); s.push(a); s.push(o); a = (Apple) (s.pop()); Run-time error

Parameterized Stack : Java Generic  No casting needed  Discover mismatches at compile-time instead of run-time CS2336: Computer Science II Stack s = new Stack (); Apple a = new Apple(); Orange o = new Orange(); s.push(a); s.push(o); compile-time error

Generic stack: linked list implementation CS2336: Computer Science II Pubic class Stack { private Node first = null; Public void push(Obj item){ //implementation } Public Obj pop() { //implementation }

Generic stack: array implementation  Generic array creation is not allowed in java  We need to use casting ( we call it ugly cast) CS2336: Computer Science II Pubic class Stack { private Obj[] stackArray; Public Stack(int size){ stackArray = (Obj[]) new Object[size]; } Public void push(Obj item){ //implementation } Public Obj pop() { //implementation }

Iterators  Allow the user to iterate through any collection  Java provides Iterable interface  Make the data structure like stack implement the Iterable interface CS2336: Computer Science II

Iterable interface  It is an interface.  Has a method that returns an Iterator.  Iterator has methods:  hasNext()  next()  Java support elegant client code if we make data structures Iterable.  “foreach” statement that is so compact CS2336: Computer Science II for (String s: Stack) System.out.println(s); Iterator i = stack.iterator(); While(i.hasNext()){ String s = i.next(); System.out.println(s); } Equivalent code

LinkedList Stack CS2336: Computer Science II import java.util.Iterator; pubic class Stack implements Iterable { …. public Iterator iterator(){ return new ListIterator(); } //inner class private class ListIterator implements Iterable { private Node current = first; public boolean hasNext(){return current!=null;} public Obj next(){ Obj item = current.item; current = current.next; return item; }

Array Stack CS2336: Computer Science II import java.util.Iterator; pubic class Stack implements Iterable { …. public Iterator iterator(){ return new ReverseArrayItr(); } //inner class private class ReverseArrayItr implements Iterable { private int i = topOfArray; public boolean hasNext(){return i > 0; } public Obj next() {return stackArray[--i]; } }

Java Collection Library  List interface  java.util.List is API for sequence of items  List interface also extends Iterable  java.util.ArrayList uses resizable array  java.util.LinkedList uses Linked list  Why not just use the java library ??  The problem is that a lot of operations get added, and API become bloated. It is not a good idea to have lots of operations to the same API  We don’t know much about the performance! CS2336: Computer Science II

Example Generate random open sites in an N-by-N percolation System 1. Use an array: pick (i,j) at random ; if already open, repeat. Takes linear time o(n) 2. use java.util.Linkedlist pick an index at random and delete takes quadratic time (n 2 ) Reason: Java LinedList implementation take linear time to find an item within given index. Not constant time like an array. Don’t use a library until you understand its API. CS2336: Computer Science II