Java Programming: Guided Learning with Early Objects Chapter 12 Generics, Dynamic Representations,...

download Java Programming: Guided Learning with Early Objects Chapter 12 Generics, Dynamic Representations, and Collections

of 50

  • date post

    26-Mar-2015
  • Category

    Documents

  • view

    217
  • download

    1

Embed Size (px)

Transcript of Java Programming: Guided Learning with Early Objects Chapter 12 Generics, Dynamic Representations,...

  • Slide 1

Java Programming: Guided Learning with Early Objects Chapter 12 Generics, Dynamic Representations, and Collections Slide 2 Java Programming: Guided Learning with Early Objects2 Objectives Learn about generic methods and classes Learn about the interface Comparable and how to implement it Learn about the Java class LinkedList and how to use various operations of this class Slide 3 Java Programming: Guided Learning with Early Objects3 Objectives (continued) Explore iterators and how they work Learn about the interface Collection and its framework Learn about various collection algorithms Slide 4 Java Programming: Guided Learning with Early Objects4 Generic Methods Generic methods: write one method definition rather than multiple definitions Defined using type parameters Type parameter section precedes return type of the method Type parameters separated by commas Enclosed in angle brackets Also called type variables Slide 5 Java Programming: Guided Learning with Early Objects5 Generic Methods (continued) Type parameters used for: Declaring return type of the method Declaring formal parameters Declaring local variables Type parameters represent only reference parameters Not primitives modifiers returnType methodName (formal parameter list) { // Method body } Slide 6 Java Programming: Guided Learning with Early Objects6 Generic Classes Type parameters are enclosed in angle brackets Placed after the name of the class Type parameter declares variables and generic methods within the class modifiers className modifiers { //class members } Also known as parametric classes Slide 7 Java Programming: Guided Learning with Early Objects7 Generic Classes (continued) Reference variables declared using type parameter T within definition of the class Cannot instantiate objects using type parameter Slide 8 Java Programming: Guided Learning with Early Objects8 The interface Comparable One method: compareTo Used to require a class to provide definition for the method compareTo Allows values of two objects to be compared Make the class implement interface Comparable Slide 9 Java Programming: Guided Learning with Early Objects9 Generic Methods and Bounded Type Parameters Recall generic method print Type parameter T refers to any built-in Java class No restriction on type parameter T Type parameters restricted, for example, to classes that define compareTo Example: public static > T larger( T x, T y ) Slide 10 Java Programming: Guided Learning with Early Objects10 Generic Methods and Bounded Type Parameters (continued) Consider: > T refers to any class that implements Comparable Comparable is the upper bound of the type parameter T By default, Object always an upper bound Keyword extends used when type parameter declaration bounds a parameter Slide 11 Java Programming: Guided Learning with Early Objects11 Generic Search and Sort Algorithms (Optional) Sort algorithms from earlier chapters work only on int arrays Designing generic search and sort algorithms Generic selection sort Place definition of selection algorithm in a class Slide 12 Java Programming: Guided Learning with Early Objects12 Collections Vector known as a collection class class Vector implements array-based lists class LinkedList implements a linked list Slide 13 Java Programming: Guided Learning with Early Objects13 Linked Lists and the class LinkedList Array also called a sequential list Operations on sequential lists: Insertion Deletion Searching Searching unsorted data is time-consuming Insertion and deletion is time-consuming on sorted lists Array size is fixed Slide 14 Java Programming: Guided Learning with Early Objects14 Linked Lists Linked list: collection of components called nodes Each node contains address of next node Two parts: One stores relevant information One stores address Address of first node is called head or first class Node defines the node of a linked list Two instance variables: info and link Slide 15 Java Programming: Guided Learning with Early Objects15 Figure 12-2 Linked list Figure 12-3 Linked list and values of the links Slide 16 Java Programming: Guided Learning with Early Objects16 Linked Lists: Some Properties Keep a reference variable current to point to address of the current node in the list Useful for traversing the list Slide 17 Java Programming: Guided Learning with Early Objects17 Figure 12-4 Linked list with four nodes Slide 18 Java Programming: Guided Learning with Early Objects18 Table 12-1 Values of head and Some of the Nodes of the Linked List in Figure 12-4 Slide 19 Java Programming: Guided Learning with Early Objects19 Figure 12-5 Linked list after the statement current = current.link; executes Slide 20 Java Programming: Guided Learning with Early Objects20 Table 12-2 Values of current and Some of the Nodes of the Linked List in Figure 12-5 Slide 21 Java Programming: Guided Learning with Early Objects21 Insertion Create a new node Store data in instance variable info Set instance variable link to point to following node Set previous node variable link to point to new node Slide 22 Java Programming: Guided Learning with Early Objects22 Figure 12-6 Linked list before item insertion Slide 23 Java Programming: Guided Learning with Early Objects23 Figure 12-7 Create newNode and store 50 in it Slide 24 Java Programming: Guided Learning with Early Objects24 Figure 12-8 List after the statement newNode.link = p.link; executes Slide 25 Java Programming: Guided Learning with Early Objects25 Figure 12-9 List after the statement p.link = newNode; executes Slide 26 Java Programming: Guided Learning with Early Objects26 Deletion Set link variable of previous node to link to next node in list Bypass the node to be deleted Garbage collector runs if no reference variable points to the deleted node Memory reclaimed by system Slide 27 Java Programming: Guided Learning with Early Objects27 Figure 12-10 Node to be deleted is with info 34 Slide 28 Java Programming: Guided Learning with Early Objects28 Figure 12-11 List after the statement p.link = p.link.link; executes Slide 29 Java Programming: Guided Learning with Early Objects29 Doubly Linked Lists Every node has a next pointer and a previous pointer Can be traversed efficiently in either direction Can traverse from first or last node Inserting a node requires setting pointers in both directions Deleting a node requires bypassing a node in both directions Slide 30 Java Programming: Guided Learning with Early Objects30 Figure 12-12 Doubly linked list Slide 31 Java Programming: Guided Learning with Early Objects31 The class LinkedList class LinkedList implemented as a doubly linked list Every element points to immediate predecessor and successor Two constructors: Collection Type parameter ? represents unknown type ? called wildcard Type E an upper bound of the wildcard Slide 32 Java Programming: Guided Learning with Early Objects32 Iterators Process elements of a collection one by one Could use foreach loop Use an iterator Processes elements in collection one after the other interface ListIterator implements interface Iterator interface Iterator has only three methods Slide 33 Java Programming: Guided Learning with Early Objects33 Table 12-4 Methods of the interface Iterator Slide 34 Java Programming: Guided Learning with Early Objects34 Table 12-5 The interface ListIterator and its Methods Slide 35 Java Programming: Guided Learning with Early Objects35 Table 12-5 The interface ListIterator and its Methods (continued) Slide 36 Java Programming: Guided Learning with Early Objects36 Iterators (continued) Typically, iterator moves in only one direction Starts at first element, moves to last class LinkedList implemented as doubly linked lists Iterator should move in both directions interface ListInterface extends interface Iterator Has methods to move in both directions Can also be used for insertion and deletion Slide 37 Java Programming: Guided Learning with Early Objects37 Collections and Collection Algorithms Collection: data structure that holds references to other objects Examples: vector, array Collection class: a class whose objects are collections Example: class Vector Several interfaces and classes implement collections systematically Hierarchical structure Slide 38 Java Programming: Guided Learning with Early Objects38 Figure 12-13 Some Java Collection classes and the inheritance hierarchy Slide 39 Java Programming: Guided Learning with Early Objects39 Table 12-6 Methods of the Collections class Slide 40 Java Programming: Guided Learning with Early Objects40 The Method addAll Method addAll adds the elements of a collection into another collection List to be added is a variable length parameter Elements specified individually or as an array Throws: UnsupportedOperationException, NullPointerException, IllegalArgumentException Slide 41 Java Programming: Guided Learning with Early Objects41 The Methods binarySearch, sort, and reverseOrder binarySearch : search the list, specified as parameter for object Efficiently searches a list for a given element Requires a list to be sorted List can be sorted in ascending or descending order using reverseOrder sort : type parameter T bounded by interface Comparable Slide 42 Java Programming: Guided Learning with Early Objects42 Table 12-7 Methods of the interface Comparator Slide 43 Java Programming: Guided Learning with Early Objects43 The Method copy Copies elements of one collection into another collection object After copy operation, index of each copied element is identical in source and destination Copies only references of the objects Objects in destination list point to same objects as source Objects in source list are mutable Changing value of object by using source changes value in destination Slide 44 Java Programming: Guided Learning with Early Objects44 The Methods max and min Methods max and min determine maximum and minimum elements All elements in collection