Title: Chapter 5 Java Objects and Iterators
1Chapter 5Java Objects and Iterators
- CS 260 Data Structures
- Indiana University Purdue University Fort Wayne
- Mark Temte
2Java Objects and Iterators
- Chapter objectives
- Review Java casting
- Review wrapper classes
- Develop generic collection classes to hold any
kind of object - Review the Java interface
- Develop a generalized iterator class
3Java casting
- Class String is a subclass of class Object
- A String object s is also an Object
- However, an Object obj may or may not be a String
Object
Throttle
Vector
String
Stack
4Java casting
String s new String( Message ) Object
obj obj s // This is a widening conversion
(automatic in Java) s obj //
Compiler error s (String) obj
// This is a narrowing conversion
- A narrowing conversion requires an explicit
typecast - The narrowing conversion produces a runtime error
if obj does not refer to a String object - The compiler takes the programmers word for the
validity of the explicit typecast - The virtual machine checks the validity
5Wrapper classes
- How can we have Object obj refer to a primitive
value? - Answer Use a wrapper class
int i 7 // i is a primitive
value Integer w new Integer( i ) //
Integer is the int wrapper class obj w
// Widening
conversion int j w.intValue( )
// Extracts the int value
7
anInteger
i w obj
7
6Wrapper classes
- Wrapper classes are not convenient
- The operator cannot be used to combine
objects - To add w and w2 . . .
- Note J2SE 1.5 specifies automatic boxing and
unboxing - Automatic conversion between primitive and
wrapper values - w2 3 and w w w2 would compile and execute
properly
Integer w2 new Integer( 3 ) // Another int
wrapper w w w2
// Causes a compiler error
w new Integer( w.intValue() w2.intValue() )
7Generic collections
- Instead of developing a separate collection class
for each kind of object, it is advantageous to
design a collection class for any kind of object - For example, replace IntArrayBag and
DoubleArrayBag and StringArrayBag by a generic
ArrayBag - An ArrayBag instance should hold Objects
- A generic ArrayBag class is developed in the
text, pages 253 264 - The technique is summarized on the next slide
- Study the details on your own
8Conversion of the IntArrayBag class to a generic
ArrayBag class
- Change references to bag elements from int to
Object - Note that some int variables like manyItems
remain int - Change equality tests among elements from x y
or x ! y to x.equals( y ) and !x.equals( y ) - Only make this change when data are being
compared - Decide if null is ever allowed as an element
- If so, then equals( ) cannot be applied to null
elements - Set unused pointers to null
- Then garbage collection can recycle unused
storage - The remove method needs to do this
9Generic collections
- Note J2SE 1.5 has features to implement generic
classes - Example
- Now variable a represents a collection capable of
holding int elements - The automatic boxing and unboxing of J2SE 1.5
facilitates use of this ArrayBag
public class ArrayBaglt E gt implements Collectionlt
E gt . . . ArrayBagltIntegergt a new
ArrayBagltIntegergt( )
10The predefined Vector class
- The Vector class may be imported using
- import java.util.Vector
- A Vector object resembles an array of Objects
- Capacity automatically grows to accommodate the
largest index - Some operations . . .
Vector v new Vector() // instantiates a
Vector v.addElement( someObject ) // adds at
end v.indexOf( someObject ) // seaches using
equals v.setElementAt( someObject, 11 ) //
assigns to position 11
11Java interfaces
- Recall that Java defines interfaces
- An interface requires that the methods specified
by the interface be implemented by any class that
implements the interface - Standard interfaces are well known
- If you know that a certain class implements a
standard interface, then you can rely on the
specified methods being available - Format
public class MyClass implements SomeInterface
12The Iterator interface
- Java 1.2 has an interface named Iterator
- Behavior of Iterator
- hasNext( )
- next( )
- remove( )
- Any class implementing the Iterator interface
must implement these methods - These methods are what are needed to iterate over
a collection
13The Lister class
- We develop a generalized iterator class named
Lister - Objects of type Lister will be external iterators
that iterate over collections built with linked
lists - An external iterator maintains its own copy of
the cursor - The collection itself doesnt need a cursor
variable - An external iterator allows multiple iterations
to occur over some collection object
simultaneously - Perhaps you have nested loops
- Each loop needs to separately iterate over the
collection - Just define as many iterator objects as you need
14The Lister class
- The Lister class we will develop works with
generic linked lists built out of Nodes - Node class
- Like class IntNode except the data type is Object
instead of type int - See Appendix E of text, pages 766 773
15The Lister class
- ADT invariant of the Lister class
- The instance variable list is the head reference
for the linked list that - contains the elements that have not yet been
provided by the next method - If there are no more elements to provide, then
list is the null reference
import java.util.Iterator public class Lister
implements Iterator // private Node
list // list is a cursor for the linked list
of some collection // public Lister( Node
head ) list head public
boolean hasNext( ) return ( list
! null )
16The Lister class
// Returns the next element and advances the
cursor to the subsequent element public Object
next( ) Object answer if ( !
hasNext( ) ) throw new
NoSuchElementException( "The Lister is empty )
answer list.getData( ) list
list.getLink( ) return answer //
// We dont want the remove method, but we have
to implement something public void remove( )
throw new UnsupportedOperationException(
"Lister has no remove method ) // end
class Iterator
17Using the Lister class
- We still need to tie the Lister instance variable
link to a node in the linked list - Suppose generic class LinkedBag is being used
- Like IntLinkedBag but elements are of class
Object - We will place int values in the LinkedBag in
wrappers - See Appendix F in the text, pages 774 779
- We need to add a method to LinkedBag that
attaches the external iterator to the linked list
within the LinkedBag - This method is iterator( )
public Lister iterator( ) return new Lister(
head )
18Using the Lister class
- Now we can iterate over LinkedBag objects
LinkedBag b new LinkedBag( ) Lister lis int
sum 0 b.add( new Integer( 11 ) ) b.add( new
Integer( 7 ) // Iterate over the bag and
add up all the ints lis b.iterator( )
// this attaches lis to bs linked
list while ( lis.hasNext( ) ) sum sum
lis.next( ).intValue( )