Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet.

Similar presentations


Presentation on theme: "1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet."— Presentation transcript:

1 1 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 3 rd : nd 3.2,3.4,3.5

2 2 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)

3 3 Parameterized Classes Defining a parameterized class: public class Generic { // use T in attribute declarations private T whatIsThis; // use T as a method’s parameter type public void doThis(T input) { … } // use T as a method’s return type public T doThat( … ) { return new T( … ); }

4 4 Parameterized Classes Instantiating a parameterized class Generic g = new Generic (); 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 missing () (second edition page:70) Box box1 = new Box (); Box box2 = new Box ();

5 5 Parameterized Classes Note: Primitive types can not be used as a generic type: Generic g =... // OK Generic g =... // Compile error Solution ^^

6 6 Parameterized Classes Must use a known class - not dummy letters Generic g = new Generic (); // error Unless in a generic class where T is defined public class AnotherGenericClass { … Generic g = new Generic (); // OK … }

7 7 Parameterized Classes Don’t omit an identified 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

8 8 Parameterized Classes Can’t instantiate arrays of the generic type without using a “trick” T [] t = new T[10]; // compile error T [] t = (T []) new Object[10]; // OK Can’t instantiate arrays of a parameterized class without using a slightly different “trick” ArrayList [] a = (ArrayList []) new ArrayList[10]; –Not a good programming practice.

9 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 BadClass { private static T count; // error public static T method() // error {…} }

10 10 Parameterized Classes Don’t invoke static methods of parameterized classes with a generic type specified BadClass b = new BadClass (); // the following is an error int n = BadClass.method(); // the method must be invoked as int n = BadClass.method();

11 11 Parameterized Methods Individual static methods in a non-parameterized class can be identified as generic public class Test { public static List reverse (List in) { // use the generic type T as usual List out = new ArrayList ();... return out; }

12 12 Parameterized Methods To invoke a parameterized method you use the parameterized types but still omit generic type indicator on the method call: ArrayList shelf = new ArrayList ();... List reverse = Test.reverse(shelf);

13 13 Set ADT Set Definition Operations Our Implementation (Using Array) Reading 2 nd :3.2,3.4,3.5 – 3 rd : 15.1,15.3 Making some changes to the books code –We are going to use the SetADT interface. We change the SetADT interface to extends the Iterable interface.

14 14 Interface SetADT SetADT is an interface defining the operations of the Set ADT: void add (T element) T removeRandom( ) T remove (T element) SetADT union(SetADT set) boolean contains(T target) boolean equals(SetADT set) boolean isEmpty() int size() Iterator iterator() String toString()

15 15 ArraySet Class The ArraySet class needs to implement the Set interface with a generic type class ArraySet implements SetADT It uses an underlying array as a data structure (as its name specifies) It needs to implement code for all the methods defined in the SetADT and Iterable interfaces.

16 16 ArraySet Underlying Data Structure private static Random rand = new Random(); private static final int DEFAULT_CAPACITY = 100; private int count; private T[ ] contents; contents[0]contents[1]contents[2]contents[3]contents[4]contents[5] Elements are kept contiguous null


Download ppt "1 ADT and Data Structure Example Generics / Parameterized Classes Using a Set Implementing a Set with an Array Example: SetADT interface Example: ArraySet."

Similar presentations


Ads by Google