Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I

2 Containers Bag Set Stack Queue Sorted List . . . Array Linked List

3 Iterators Step through a container
Array Circular array Sorted Array Linked List Without knowing the details of the container For users to use

4 Why Iterators? public class StudentList { public float avgGPA() . . } After the class is completed, the application wants to know the average score for the final exam of CS 2430 Modify class StudentList The application utilizes an Iterator if it’s implemented!

5 Java Iterator Interface
// Only two methods public Interface Iterator { // The class implementing the interface should have // a pointer to the next object in the container // Returns true if next() can be successfully called public boolean hasNext(); // Returns the next object and // moves the pointer to the following object public Object next(); }

6 Different Types of Iterators
External Internal Embedded

7 External Iterators import java.util.Iterator; public class List {
} public class MyIter implements Iterator Advantage Can have several iterators over the same container at the same time Disadvantage The iterator class needs to get at the details of the container class.

8 Internal Iterators import java.util.Iterator;
public class List implements Iterator { } Advantage Direct access to the container class. Disadvantage Can have only one iterator over the same container at one time

9 Embedded Iterators import java.util.Iterator; public class List {
public static class ListIterator implements Iterator } Combine the advantages of internal and external iterators Direct access to the container class. Can have several iterators over the same container at the same time

10 Embedded Iterators on Array
public class List { private Object [] elements; private int num; public List ( int initialSize ) elements = new Object[ initialSize ]; num = 0; } .... All the other nice list methods .... could be growable public static class ListIterator implements Iterator

11 private Object [] elements; // regular array, not circular
public class List { private Object [] elements; // regular array, not circular private int num; public static class ListIterator implements Iterator private List theList; private int index; // index of the next object public ListIterator ( List list ) theList = list; index = 0; } @Override public boolean hasNext() return index < theList.num; public Object next() return theList.elements[index ++];

12 Example List myList = new List(1000); Iterator iter = new List.ListIterator(myList); while ( iter.hasNext() ) System.out.println ( iter.next() );

13 Generic Iterator Interface
// Only two methods public Interface Iterator<E> { // The class implementing the interface should have // a pointer to the next object in the container // Returns true if next() can be successfully called public boolean hasNext(); // Returns the next object and // moves the pointer to the following object public E next(); }

14 Prog 3: FixedPointList public class FixedPointList { private static final int GROWBY = 3; private int num = 0; private FixedPoint[] list = new FixedPoint[GROWBY]; public FixedPoint min() public FixedPoint max() public FixedPoint sum(int q) }

15 public class FixedPointList { private int num = 0; private FixedPoint[] list = new FixedPoint[GROWBY]; public static class MyIterator implements Iterator<FixedPoint> private FixedPointList theList; private int index; public MyIterator(FixedPointList list) theList = list; index = 0; public boolean hasNext() return index < theList.num; public FixedPoint next() return theList.list[index ++];

16 public class Prog3 { private static FixedPointList list = new FixedPointList(); private static void sumCmd() FixedPointList.MyIterator iterator = new FixedPointList.MyIterator(list); FixedPoint fp = new FixedPoint(0.0, curQ); while(iterator.hasNext()) fp = fp.plus(iterator.next(), curQ); System.out.println("The sum is: " + fp); }

17 private Object [] elements;
public class Queue { private Object [] elements; private int front, rear, count; // circular array public static class QueueIterator implements Iterator private Queue theQueue; private int index; // index of the next object public QueueIterator ( Queue queue ) theQueue = queue; index = ??? } @Override public boolean hasNext() return ??? public Object next()

18 public class Final { private static Queue myQueue = new Queue(1000);
public class Final { private static Queue myQueue = new Queue(1000); private static void sumCmd() Queue.QueueIterator iterator = new Queue.QueueIterator(myQueue); FixedPoint fp, sum = new FixedPoint(0.0, curQ); while(iterator.hasNext()) fp = (FixedPoint)iterator.next(); sum = sum.plus(fp, curQ); } System.out.println("The sum is: " + fp);

19 Test 3 Wednesday, Dec 12 Please Fill out the Course Outcome Survey!
Schedule Test 3 Wednesday, Dec 12 Please Fill out the Course Outcome Survey!

20 Test 3 Big O Counting Sorting Hashing Binary Search LSP
(Iterator will be on the final exam)

21 Prog 6


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google