Download presentation
Presentation is loading. Please wait.
Published byAlexina Hodges Modified over 9 years ago
1
Today’s Agenda Generic Iterators CS2336: Computer Science II
2
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
3
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
4
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 }
5
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 }
6
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
7
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
8
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; }
9
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]; } }
10
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
11
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.