Docsity

Lecture Notes on Iterators | Introduction to Data Structures | COMP SCI 367, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Copyright 2014 Jim Skrentny CS 367 L4 - 1
CS 367 Announcements
Thursday, January 30, 2014
Email [email protected] by this Friday if you:
• have a conflict with any of the exams listed on course website
• participate in religious observances that may interfere with course requirements
• have a VISA from the McBurney Disability Resource Center
Course Website - http://pages.cs.wisc.edu/~cs367-1/
Sign Up for Piazza
Homework 1 assigned tomorrow
Program 1 assigned tomorrow
Last Time
• List ADT
• coding as a Java interface
• using Lists
• implementing using an array (SimpleArrayList)
• Lists and the Java API
Today
Iterators
• concept
• iterators and the Java API
• using iterators
• options for implementing iterators
• making a class iterable
Next Time
• Review Exceptions
<<Study this for exam(?)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Lecture Notes on Iterators | Introduction to Data Structures | COMP SCI 367 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 367 Announcements

Thursday, January 30, 2014

Email [email protected] by this Friday if you:

  • have a conflict with any of the exams listed on course website
  • participate in religious observances that may interfere with course requirements
  • have a VISA from the McBurney Disability Resource Center

Course Website - http://pages.cs.wisc.edu/~cs367-1/

Sign Up for Piazza

Homework 1 assigned tomorrow

Program 1 assigned tomorrow

Last Time

  • List ADT
  • coding as a Java interface
  • using Lists
  • implementing using an array (SimpleArrayList)
  • Lists and the Java API

Today

Iterators

  • concept
  • iterators and the Java API
  • using iterators
  • options for implementing iterators
  • making a class iterable

Next Time

  • Review Exceptions

Iterators

What are they?

Concept

Operations

Using Iterators

Suppose words is a SimpleArrayList that implements the Iterable Interface.

Write a code fragment that gets an iterator, named itr, from words.

Suppose words is a SimpleArrayList and itr is an iterator for words.

Write a code fragment that uses itr to print each item in words.

Using Iterators

Assume SimpleArrayList implements the Iterable Interface.

Complete the method using iterators to determine list contains duplicates.

public boolean hasDups(SimpleArrayList list) {

Option 1: Implementing an Indirect Access ArrayBagIterator Class

(as done SimpleArrayList in readings)

import java.util.*;

public class ArrayBagIterator implements Iterator {

public ArrayBagIterator( ) {

}

public boolean hasNext() {

}

public E next() {

}

public void remove() {

}

}

Option 2: Implementing a Direct Access ArrayBagIterator Class

import java.util.*;

public class ArrayBagIterator implements Iterator {

public ArrayBagIterator( ) {

}

public boolean hasNext() {

}

public E next() {

}

public void remove() {

}

}

List ADT

Concept

Coding ListADT using a generic Java interface

public interface ListADT {

void add(E item);

void add(int pos, E item);

boolean contains(E item);

int size();

boolean isEmpty();

E get(int pos);

E remove(int pos);

}

Implementing ListADT using a generic array-based class

public class SimpleArrayList implements ListADT{

private E[] items; // items in the List

private int numItems; // # of items in the List

public SimpleArrayList() { ... }

//*** required ListADT methods ***

public void add(E item) { ... }

public void add(int pos, E item) { ... }

public E remove(int pos) { ... }

public E get (int pos) { ... }

public boolean contains (E item) { ... }

public int size() { ... }

public boolean isEmpty() { ... }

//*** private methods ***

}