COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
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++)
Advertisements

Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
CS 280 Data Structures Professor John Peterson. Project 9 Questions? IS280.
Iterators CS 367 – Introduction to Data Structures.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Information and Computer Sciences University of Hawaii, Manoa
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Information and Computer Sciences University of Hawaii, Manoa
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
COMP 121 Week 11: Linked Lists.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
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.
IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.
Week 15 – Monday.  What did we talk about last time?  Tries.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Linked Data Structures
Iterators.
Elementary Data Structures
The List ADT.
Week 4 - Monday CS221.
UMBC CMSC 104 – Section 01, Fall 2016
Week 4 - Friday CS221.
Linked Lists Chapter 5 (continued)
Week 15 – Monday CS221.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Top Ten Words that Almost Rhyme with “Peas”
CS 1430: Programming in C++.
THURSDAY, OCTOBER 17 IN LAB
Building Java Programs
Collections A First Glimpse.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
(Java Collections Framework) AbstractSequentialList
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CSE 143 Lecture 27: Advanced List Implementation
CS 2430 Object Oriented Programming and Data Structures I
slides adapted from Marty Stepp
searching Concept: Linear search Binary search
COMPUTER 2430 Object Oriented Programming and Data Structures I
Implementing Hash and AVL
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CSE 214 – Computer Science I More on Linked Lists
Lists and the Collection Interface
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
Given value and sorted array, find index.
Interator and Iterable
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 4 Queues.
JCF Collection classes and interfaces
CS 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Linked Lists Chapter 5 (continued)
Collections A First Glimpse 16-Apr-19.
COP3530- Data Structures Introduction
Class StudentList class StudentList { private: int numStudents;
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Java Generics & Iterators
COMPUTER 2430 Object Oriented Programming and Data Structures I
Data Structures & Programming
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

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

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

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!

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(); }

Different Types of Iterators External Internal Embedded

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.

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

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

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 . . . . .

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 ++];

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

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(); }

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) . . . }

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; } @Override public boolean hasNext() return index < theList.num; public FixedPoint next() return theList.list[index ++];

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); }

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()

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);

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!

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

Prog 6