Sie sind auf Seite 1von 18

ADT and Data Structure Example

Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet class ArraySet Underlying Data Structure Reading: L&C 15.3
1

Java Generics
In Java 5.0, Sun added a generic feature that had been available in other languages such as C++ (where it is called Templates) Although the implementation is different, the user gains the benefit of stronger compiler type checking and simpler coding style (with a reduced number of explicit casts needed)

Java Generics
The Java implementation was constrained by backward compatibility Code compiled using javac 5.0 and generics had to work on java 4.2 virtual machines The technique used was type erasure and one copy of the parameterized class code is accessed with compiler supplied casts Avoids C++ code bloat where there are multiple copies of the parameterized code
3

Parameterized Classes
Defining a parameterized class:
public class Generic<T> { // use T in attribute declarations private T whatIsThis; // use T as a methods parameter type public void doThis(T input) { } // use T as a methods return type public T doThat( ) { return new T( ); } }

Parameterized Classes
Instantiating a parameterized class
Generic<String> g = new Generic<String>();

Use methods with objects of the actual type


g.doThis(Hello); String s = g.doThat( );

No casting of data types should be required Note: Error in text on pgs 40-41 - missing ()
Box<Widget> box1 = new Box<Widget>(); Box<Gadget> box2 = new Box<Gadget>();
5

Parameterized Classes
Note: The letter used inside <> is a dummy and can be <T> like C++ or <E> like Sun I prefer to use T based on C++ popularity and that is also what our textbook uses Only reference types - not primitive types can be used as a generic type
Generic<String> g = . . . // OK Generic<int> g = . . . // Compile error
6

Parameterized Classes
Must use a known class - not dummy letters
Generic<T> g = new Generic<T>(); // error

Unless in a generic class where T is defined


public class AnotherGenericClass<T> { Generic<T> g = new Generic<T>(); // OK }
7

Parameterized Classes
Dont omit an identified <type> in new code
Generic g = new Generic(); // legacy code?

Compiler will give incompatible type errors without an explicit cast (narrowing)
String s = g.doThat( ); // error String s = (String) g.doThat( ); // OK

Compiler will give unchecked warnings


g.doThis(Hello); // warning

Parameterized Classes
Cant instantiate arrays of the generic type without using a trick
T [] t = new T[10]; // compile error T [] t = (T []) new Object[10]; // OK

Cant instantiate arrays of a parameterized class without using a slightly different trick
ArrayList<String>[] a = (ArrayList<String>[]) new ArrayList[10];

Casting a new Object[10] compiles OK, but throws an exception at run time 9

Parameterized Classes
Static members can not be generic because there is only one copy of the code for all of the parameterized class objects instantiated with possibly different generic types
public class BadExample<T> { private static T count; // error public static T method() // error {} }
10

Parameterized Classes
Dont invoke static methods of parameterized classes with a generic type specified
BadExample<Integer> b = new BadExample<Integer>(); // the following is an error int n = BadExample<Integer>.method(); // the method must be invoked as int n = BadExample.method();
11

Using a Set
First, we need a defined interface for a collection to contain our set of objects See SetADT<T> interface definition (L&C pg 437) Next, we need to define the class that will be contained in the collection See BingoBall class definition (L&C pg 441) Then, we need to define a class with code to create/manipulate BingoBall objects See Bingo class definition (L&C pg 442)
12

Using a Set
Finally, we need a class that implements the SetADT<T> interface to contain our BingoBall objects including two things:
A selection for an underlying data structure Code to implement the SetADT methods using the chosen underlying data structure

A UML diagram for the Bingo application is shown in the next slide
13

UML Diagram for Bingo Application


<<interface>> Iterable<T> extends <<interface>> SetADT<T>

implements Bingo uses ArraySet<BingoBall>

+ main (args : String[ ]) : void uses

BingoBall
NOTE: More complete than the UML diagram provided in L&C on page 440. See subsequent slides for difference.

14

Interface SetADT<T>
SetADT<T> is an interface defining all the methods the Bingo class needs to use
void add (T element) T removeRandom( ) T remove (T element) SetADT<T> union(SetADT<T> set) boolean contains(T target) boolean equals(SetADT<T> set) boolean isEmpty() int size() Iterator<T> iterator() String toString()

15

Interface SetADT<T>
SetADT includes the iterator method, but the authors code doesnt extend Iterable My preference for definition of the SetADT interface would be for it to extend Iterable
interface SetADT<T> extends Iterable<T>

Then, the new Java 5.0 for-each loop can be used on any object of type SetADT and the ArraySet class does not need separate specification of implements Iterable I will show some benefits of doing this later
16

ArraySet<T> Class
The ArraySet class needs to implement the SetADT interface with a generic type
class ArraySet<T> implements SetADT<T>

It uses an underlying array as a data structure (due to the use of the word array in its name) It needs to implement code for all the methods defined in the SetADT interface In these slides, I will show some code that is different from the code in the book so we can discuss alternative ways of writing the code17

ArraySet Underlying Data Structure


private static Random rand = new Random(); private static final int DEFAULT_CAPACITY = 100; private int count; private T[ ] contents; Value of count is the next index in array to be used for adding objects

contents[0] contents[1] contents[2] contents[3] contents[4] contents[5]

null

null

null

Elements are kept contiguous

18

Das könnte Ihnen auch gefallen