The Iterator Interface and Vectors - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

The Iterator Interface and Vectors

Description:

In Java 1.2 (not in VJ ) Very similar. public interface Iterator { boolean hasNext ... Why the Interface construct? More data encapsulation. ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 37
Provided by: anselmo9
Category:

less

Transcript and Presenter's Notes

Title: The Iterator Interface and Vectors


1
The Iterator Interfaceand Vectors
  • COMP114
  • Tuesday February 19

2
Announcements
  • Program 2
  • Use encapsulation (getters/setters)
  • By the way, program would not be done this way
  • Start small (maybe begin with my callback
    example), build it up and keep it working
  • Ok to use my code, with attribution
  • I/O
  • Lewis Loftus 8.2-8.5
  • Weiss 2.6

3
Design Patterns
  • The idea is to catalog designs that have worked
    well.
  • Giving a name to something also makes it easier
    to talk about and explain.
  • The pattern is a description of the core problem
    and solution.
  • The key is that these are recurring problems.

4
The Iterator Pattern
  • Very simple pattern (maybe simplest)
  • Problem domain
  • When you need to access a list
  • One element after another
  • Solution strives to
  • Hide how it's implemented
  • Hide how list is stored

5
Functionality of Iterator
  • Possible methods
  • first ( )
  • hasNext( )
  • getNext( )
  • Maybe also
  • getCurrent( )

6
Recall Java Interfaces
  • An interface specifies a set of methods that must
    be supplied.
  • Java Interface
  • You can then use the name of the interface as the
    reference data type

7
Java Iterator Enumeration
  • In package java.util.Enumeration
  • Methods
  • hasMoreElements( )
  • nextElement( )
  • Exception
  • NoSuchElementException

8
Enumeration Interface
  • public interface Enumeration
  • boolean hasMoreElements()
  • Object nextElement()

Show in J Help in java.util
9
Example Use
  • import java.util.
  • public class Names implements Enumeration
  • ...
  • public boolean hasMoreElements()
  • ...
  • public Object nextElement()throws
    NoSuchElementException
  • ...

10
Code Example
  • Project Enumeration
  • Show simple use
  • Cause exception by reading off end of list.
  • Show that you must convert from Object to String

11
Warning
  • Notice that iterator is invalid if something has
    changed the list of items
  • Be careful not to cause inconsistencies

12
Java Iterator Interface
  • In Java 1.2 (not in VJ)
  • Very similar
  • public interface Iterator
  • boolean hasNext()
  • Object next()
  • void remove()

13
Remove?
  • What if you don't want remove( )?
  • Like Program 1
  • The specification says
  • Throws UnsupportedOperationException if the
    remove operation is not supported by this
    Iterator.

14
Why the Interface construct?
  • More data encapsulation.
  • It doesn't matter to the code calling the
    Enumeration what kind of object is sending the
    information.
  • Could be a Vector (list of stored items)
  • Could be List
  • Or more advanced data structure

15
Multiple Inheritance
  • Back to abstract class vs. interface
  • Some languages allow inheritance from multiple
    superclasses
  • A dictionary (say)
  • Also an iterator
  • Problem it gets very complicated
  • Interface is Java's way to give part of this.

16
Vector
  • The Java name for a class similar to arrays but
    it can grow in size
  • You used fixed-length arrays on Program 1 (you
    knew there were at most 4 cities) and Program 2.
  • Let's see how to do this right.
  • See alternative ways later in semester

17
First Try A Big Array
  • String tokens
  • new String10000000
  • Problem is that you may not need anything that
    big.
  • Wasteful and you still may run out!

18
Next Get User to Tell You
  • Make user input size as the first element.
  • Very inelegant.
  • You've just put the burden on someone else.
  • Sometimes just not possible.

19
Vector
  • Just grow arrays as needed.
  • In Java a Vector class provides a more flexible
    array
  • Vector is documented in java.util
  • Include import java.util in file

20
Subset of Vector interface
  • Constructor Vector( int capacity )
  • addElement( Object obj ) to end
  • elementAt( int index )
  • setElementAt( Object obj,
  • int index ) replaces Object at index
  • removeElement( int )

21
Vector Not Just Array That Grows!
  • Inserts in the middle
  • insertElementAt( Object obj,
  • int index )
  • Removes from the middle (shrinks array)
  • removeElementAt( int index )
  • Imagine an array implementation

22
addElement( )
  • May need to add space
  • public void addElement(Object obj)
  • ensureCapacity(elementCount1)
  • elementDataelementCount obj
  • elementCount

23
Inserting Elements?
  • To insert (not just replace) a new element need
    to shift entries down.

24
insertElementAt( )
  • public void insertElementAt(Object obj,
  • int index)
  • int i
  • ensureCapacity(elementCount1)
  • for (i elementCount i gt index i--)
  • elementDatai elementDatai-1
  • elementDataindex obj
  • elementCount

Why Count Backwards?
25
Forward Copy
26
Backward Copy
27
ensureCapacity( )
  • public void ensureCapacity(
  • int minCapacity)
  • if (elementData.length lt minCapacity)
  • int newLength elementData.length
  • if (newLength 0)
  • newLength 1
  • while (newLength lt minCapacity)
  • newLength 2

28
Now Copy Elements
  • Object newElementData
  • new ObjectnewLength
  • int i
  • for (i 0 i lt elementCount i)
  • newElementDatai elementDatai
  • elementData newElementData

29
Why Double Size?
  • Could just increment by some fixed number
  • Problem is copy cost

30
Increment Size by One
  • 1 2 3 4 5 6 (n 1) ? n2
  • Ends up copying a lot.

31
Doubling
  • Doubling is a nice compromise
  • Imagine Vector starts at size 1
  • Increasing to 2 copies 1 element
  • Increasing to 4 copies 2 elements
  • Increasing to 8 copies 4 elements

32
Eventually
  • 1 2 4 8 n / 2 n 1
  • So eventual capacity of n causes about n copies.
  • Cost proportional to size.
  • Not bad.

33
Add Enumeration to Vector
  • class VectorIterator implements Enumeration
  • private Vector theVector
  • private int current
  • public VectorIterator(Vector v)
  • theVector v current 0
  • public boolean hasMoreElements()
  • return current lt theVector.size()
  • public Object nextElement()
  • return theVector.elementAt(current)

34
Clean Design
  • Design Pattern like this makes code
  • Easier to write
  • Easier to understand
  • Reusable

35
References
  • Design Patterns by Gamma, Helm, Johnson, and
    Vlissides. Addison Wesley.
  • Java Design Patterns by Cooper. Addison Wesley.
  • Patterns in Java, Volume 1, by Grand. Wiley.

36
Tutorial
  • Multi-part tutorial on building application
  • First part discusses OOP
  • Later GUIs
  • On Sun Java web site
  • http//developer.java.sun.com/developer/onlineTrai
    ning/new2java/divelog/
Write a Comment
User Comments (0)
About PowerShow.com