1 / 16

ADT and Data Structure Example

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 : 15.3 2 nd 3.2,3.4,3.5. Java Generics.

lfurry
Télécharger la présentation

ADT and Data Structure Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. Parameterized Classes • Defining a parameterized class: public class Generic<T> { // 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. 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 () (second edition page:70) Box<Widget> box1 = new Box<Widget>(); Box<Gadget> box2 = new Box<Gadget>();

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

  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 • Don’t 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

  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<String>[] a = (ArrayList<String>[]) new ArrayList[10]; • Not a good programming practice.

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

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

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

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

  13. Set ADT • Set Definition • Operations • Our Implementation (Using Array) • Reading 2nd:3.2,3.4,3.5 – 3rd: 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. Interface SetADT<T> • SetADT<T> is an interface defining the operations of the Set ADT: 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. ArraySet<T> Class • The ArraySet class needs to implement the Set interface with a generic type class ArraySet<T> implements SetADT<T> • 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. 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] null null null Elements are kept contiguous

More Related