cs-2852 data structures lecture 7a andrew j. wozniewicz image copyright © 2010 andyjphoto.com

18
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Upload: jeffrey-allen

Post on 03-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852Data StructuresLECTURE 7A

Andrew J. Wozniewicz

Image copyright © 2010 andyjphoto.com

Page 2: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Agenda• JCF iterator interfaces

– Iterator<E>– ListIterator<E>

• JCF Iterable interfaces– Iterable<E>– Collection<E>– List<E>

• JCF Iterable Collections– ArrayList<E>– LinkedList<E>

• Introduction to Testing

Page 3: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Iterators

• An object• Traversing a collection• Hide implementation

In computer science, an iterator is an object that allows a programmer to traverse through all the elements of a collection, regardless of

its specific implementation.

Wikipedia Definition

Page 4: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

The Iterator<E> Interface

import java.util.*

interface Iterator<E> {boolean hasNext();E next();void remove(); // optional

}

Page 5: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Doubly-Linked List – getIterator()

public class DoublyLinkedListIterable<E> {

public Iterator<E> getIterator() { return new DoublyLinkedListIterator<E>(this);}

}

Page 6: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Doubly-Linked List – Iterator

public class DoublyLinkedListIterator<T>implements Iterator<T> {

DoublyLinkedListIterable<T> theList;Node<T> current;

public DoublyLinkedListIterator( DoublyLinkedListIterable<T> list) {

theList = list;reset();

}

//TODO operations}

DEMO

Page 7: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

The ListIterator<E> Interface

import java.util.*

public interface ListIterator<E> extends Iterator<E> { public boolean hasPrevious(); public E previous(); public int nextIndex(); public int previousIndex(); public void set(E o); //optional public void add(E o); //optional}

Page 8: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

The ListIterator<E> Interface Expanded

import java.util.*

public interface ListIterator<E> { boolean hasNext(); E next(); void remove(); // optional public boolean hasPrevious(); public E previous(); public int nextIndex(); public int previousIndex(); public void set(E o); //optional public void add(E o); //optional}

Page 9: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Using Iterators I

import java.util.*

class IteratorDemo { public static void main(String args[]) {

}}

Page 10: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Using Iterators II

// create an array list ArrayList<String> al = new ArrayList<String>();// add elements to the array list al.add("Charlie");al.add("Alpha");al.add("Echo");al.add("Bravo");al.add("Delta");al.add("Foxtrot");

inside main(String args[])

Page 11: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

The Iterable<E> Interface

public interface Iterable<E> { Iterator<E> iterator();}

public interface Collection<E> extends Iterable<E> { ...}

public interface List<E> extends Collection<E> { ...}

The Basis for Collection<E>, List<E>

Iterable<E><<interface>>

Collection<E><<interface>>

List<E><<interface>>

ArrayList<E><<class>>

Java Collections Framework

Page 12: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Using Iterators III

// display contents of alSystem.out.print(“Content of al: "); Iterator itr = al.iterator(); while (itr.hasNext()) {

Object element = itr.next(); System.out.print(element + " ");

}

inside main(String args[])

Page 13: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Using Iterators IV

// modify objects being iterated ListIterator<String> litr = al.listIterator();While (litr.hasNext()) { String element = litr.next(); litr.set(element + "+");}

inside main(String args[])

Page 14: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

The List<E> Interface

public interface Iterable<E> { Iterator<E> iterator();}

public interface Collection<E> extends Iterable<E> { ...}

public interface List<E> extends Collection<E> { ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); ...}

The Basis for Collection<E>, List<E>

Iterable<E><<interface>>

Collection<E><<interface>>

List<E><<interface>>

ArrayList<E><<class>>

Java Collections Framework

Page 15: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

ArrayList

Import java.util.*

List<String> al = new ArrayList<String>(); al.add("Charlie");al.remove(0);

• Resizable-array implementation of the List interface. • As elements are added to an ArrayList, its capacity grows

automatically.• Implements all optional list operations.• Permits null. • Provides methods to manipulate size of the underlying

data array. ( stack, queue, or double-ended queue).

Java Collections Framework

Page 16: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

LinkedList

import java.util.*

List<Integer> intList = new LinkedList<Integer>();intList.addLast(42);intList.addFirst(17);

• Doubly-linked list implementation of the List interface. • Implements all optional list operations.• Permits null. • Provides uniformly named methods to get, remove and

insert an element at the beginning and end of the list ( stack, queue, or double-ended queue).

Java Collections Framework

Page 17: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz

Summary• JCF iterator interfaces – Iterator<E>– ListIterator<E>

• JCF iterable interfaces– Iterable<E>– Collection<E>– List<E>

• JCF iterable Collections– ArrayList<E>– LinkedList<E>

Page 18: CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

Questions?

Image copyright © 2010 andyjphoto.com