Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Thought for the Day “In my opinion, the only person worse than a quitter is the person afraid to begin.” – John Maxwell Generic Data Structures Using Polymorphism • Consider either of the integer list classes – We will use the linked-list implementation • Very few places where it is important that we are dealing with integers only: – data member in the ListNode class – parameters for add, position and set – return type for get Replace these with Object The get and set Methods public Object get (int index) // Access an ObjectList { ... } // get public void set (int index, Object item) // Access an ObjectList { ... } // get The position Method public int position (Object item) // Find item in an ObjectList Cannot use != { ListNode curr = first; int k; for (k = 0; curr != null && !curr.data.equals(item); k++, curr = curr.next) ; // Search for item in ObjectList if (curr == null) // item was not found return -1; else return k; } // position Using the ObjectList Class • A list of integers! ObjectList iList = new ObjectList(); iList.add(new Integer(3)); iList.add(new Integer(-7)); iList.add(new Integer(56)); Note the use of the Integer wrapper class Since Java 5 • Using wrapper classes is simplified: – Auto-boxing – Auto-unboxing ObjectList iList = new ObjectList(); iList.add(3); iList.add(-7); iList.add(56); Automatically wrapped as Integer objects Using the ObjectList Class public class Student { . . . } // class Student ... ObjectList classList = new ObjectList(); classList.add(new Student()); Need to Take Care When Using get Student st; st = classList.get(5); // Retrieve sixth student Won’t work! get returns an Object: must use a type cast Student st; st = (Student)classList.get(5); // Retrieve sixth student Mixed Lists ObjectList mixed = new ObjectList(); mixed.add(3); // Integer mixed.add(new Student()); mixed.add(new Date()); mixed.add(”Hello”); // String Need to take even more care with get! int myInt; Object obj = mixed.get(k); // Get k’th object if (obj instanceof Integer) // Check type myInt = (Integer)obj; // Type cast will work else Auto-unboxes Integer object ... Java 5 Generics • Allow us to simplify the type-casts, etc. required when using polymorphism • Provide extra safety – Extensive compile-time checking • Effectively, introduce a “type parameter” – “List of things of type T” Syntax public class MyClass<T> { . . . } // class MyClass • Inside MyClass we can use T as a type name – – – – variables parameters return types etc. The GenericList Class • Start with ObjectList – Introduce type parameter – Replace Object with type parameter public class GenericList<T> { private class ListNode { public T data; public ListNode next; } // inner class ListNode . . . } // class GenericList The add Method public void add (T item, int position) // Place the new item in a GenericList { ... } // add The get and set Methods public T get (int index) // Subscript a GenericList { ... } // get public void set (int index, T item) // Subscript a GenericList { ... } // get The position Method public int position (T item) // Find item in a GenericList { . . . } // position Using Generic Classes • Must specify the actual type to be used GenericList<Student> classList = new GenericList<Student>(); . . . • Compiler checks that only Student objects are placed in classList Using Generic Classes • Type casts no longer required GenericList<Student> classList = new GenericList<Student>(); classList.add(new Student()); . . . Student st = classList.get(k); Generics • There are some more advanced features – Will see some later – Others described in documentation Summary of List ADTs • Lists of integers: – IntegerVector – based on an array – IntegerList – based on a linked list • Generic lists: – ObjectList – using polymorphism – GenericList – using Java 5 generic features The java.util Package • This standard Java package contains similar classes: – Vector and ArrayList: generic, arraybased ADTs, with “automatic growth” – LinkedList: similar to our GenericList ADT