java programming: guided learning with early objects chapter 12 generics, dynamic representations,...

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

Upload: rachel-robinson

Post on 26-Mar-2015

238 views

Category:

Documents


1 download

TRANSCRIPT

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

Java Programming: Guided Learning with Early Objects

Chapter 12Generics, Dynamic Representations,

and Collections

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

Java Programming: Guided Learning with Early Objects 2

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

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

Java Programming: Guided Learning with Early Objects 3

Objectives (continued)

• Explore iterators and how they work• Learn about the interface Collection

and its framework• Learn about various collection algorithms

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

Java Programming: Guided Learning with Early Objects 4

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

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

Java Programming: Guided Learning with Early Objects 5

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 primitivesmodifiers <T> returnType methodName

(formal parameter list) {

// Method body}

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

Java Programming: Guided Learning with Early Objects 6

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<T> modifiers {

//class members

}

• Also known as parametric classes

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

Java Programming: Guided Learning with Early Objects 7

Generic Classes (continued)

• Reference variables declared using type parameter T within definition of the class

• Cannot instantiate objects using type parameter

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

Java Programming: Guided Learning with Early Objects 8

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

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

Java Programming: Guided Learning with Early Objects 9

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 extends Comparable<T>> T

larger( T x, T y )

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

Java Programming: Guided Learning with Early Objects 10

Generic Methods and Bounded Type Parameters (continued)

• Consider:<T extends Comparable<T>>

• 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

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

Java Programming: Guided Learning with Early Objects 11

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

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

Java Programming: Guided Learning with Early Objects 12

Collections

• Vector known as a collection class • class Vector implements array-based lists• class LinkedList implements a linked list

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

Java Programming: Guided Learning with Early Objects 13

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

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

Java Programming: Guided Learning with Early Objects 14

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

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

Java Programming: Guided Learning with Early Objects 15

Figure 12-2 Linked list

Figure 12-3 Linked list and values of the links

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

Java Programming: Guided Learning with Early Objects 16

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

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

Java Programming: Guided Learning with Early Objects 17

Figure 12-4 Linked list with four nodes

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

Java Programming: Guided Learning with Early Objects 18

Table 12-1 Values of head and Some of the Nodes of the Linked List in Figure 12-4

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

Java Programming: Guided Learning with Early Objects 19

Figure 12-5 Linked list after the statement current = current.link; executes

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

Java Programming: Guided Learning with Early Objects 20

Table 12-2 Values of current and Some of the Nodes of the Linked List in Figure 12-5

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

Java Programming: Guided Learning with Early Objects 21

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

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

Java Programming: Guided Learning with Early Objects 22

Figure 12-6 Linked list before item insertion

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

Java Programming: Guided Learning with Early Objects 23

Figure 12-7 Create newNode and store 50 in it

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

Java Programming: Guided Learning with Early Objects 24

Figure 12-8 List after the statement newNode.link = p.link; executes

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

Java Programming: Guided Learning with Early Objects 25

Figure 12-9 List after the statement p.link = newNode; executes

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

Java Programming: Guided Learning with Early Objects 26

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

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

Java Programming: Guided Learning with Early Objects 27

Figure 12-10 Node to be deleted is with info 34

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

Java Programming: Guided Learning with Early Objects 28

Figure 12-11 List after the statement p.link = p.link.link; executes

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

Java Programming: Guided Learning with Early Objects 29

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

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

Java Programming: Guided Learning with Early Objects 30

Figure 12-12 Doubly linked list

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

Java Programming: Guided Learning with Early Objects 31

The class LinkedList

• class LinkedList implemented as a doubly linked list

• Every element points to immediate predecessor and successor

• Two constructors:

Collection<? extends E>– Type parameter ? represents unknown type– ? called wildcard

– Type E an upper bound of the wildcard

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

Java Programming: Guided Learning with Early Objects 32

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

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

Java Programming: Guided Learning with Early Objects 33

Table 12-4 Methods of the interface Iterator

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

Java Programming: Guided Learning with Early Objects 34

Table 12-5 The interface ListIterator and its Methods

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

Java Programming: Guided Learning with Early Objects 35

Table 12-5 The interface ListIterator and its Methods (continued)

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

Java Programming: Guided Learning with Early Objects 36

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

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

Java Programming: Guided Learning with Early Objects 37

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

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

Java Programming: Guided Learning with Early Objects 38

Figure 12-13 Some Java Collection classes and the inheritance hierarchy

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

Java Programming: Guided Learning with Early Objects 39

Table 12-6 Methods of the Collections class

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

Java Programming: Guided Learning with Early Objects 40

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

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

Java Programming: Guided Learning with Early Objects 41

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

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

Java Programming: Guided Learning with Early Objects 42

Table 12-7 Methods of the interface Comparator

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

Java Programming: Guided Learning with Early Objects 43

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

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

Java Programming: Guided Learning with Early Objects 44

The Methods max and min

• Methods max and min determine maximum and minimum elements

• All elements in collection must be mutually comparable by the comparator

• Throws exceptions:– ClassCastException– NoSuchElementException

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

Java Programming: Guided Learning with Early Objects 45

The Methods fill and frequency

• Method fill replaces all elements of a given list with given element– Type parameter T specifies type of list elements

as well as type of replacement element

• Method frequency finds the number of times a specific object appears in a collection– If collection is null, throws NullPointerException

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

Java Programming: Guided Learning with Early Objects 46

The Method replaceAll

• Replaces all occurrences of a given element with another

• Type parameter T specifies type of list elements

• Returns true if list contains one or more elements equal to replacement object

• If list or list iterator does not support set method, throws exception

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

Java Programming: Guided Learning with Early Objects 47

The Methods shuffle and swap

• Method shuffle randomly shuffles elements of a list

• Two forms: with and without random seed• All permutations occur with approximately equal

likelihood• Method swap swaps elements of list

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

Java Programming: Guided Learning with Early Objects 48

Summary

• Generic methods are defined using type parameters

• Every generic method has a type parameter section preceding the return type of the method– Type parameters separated by commas

– Enclosed in angle brackets

• Type parameters specify generic type names• Within generic method, declare reference

variables using type parameter T

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

Java Programming: Guided Learning with Early Objects 49

Summary (continued)

• Type parameter may be bounded or unbounded

• Cannot instantiate objects using type parameter• class LinkedList implemented as a doubly

linked list– Contained in package java.util

• The foreach loop iterates over LinkedList• Iterator iterates over elements of a collection

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

Java Programming: Guided Learning with Early Objects 50

Summary (continued)

• Collection is a data structure that holds references to objects

• Collection class is a class whose objects are collections

• interface Collection is the superclass of all Java collection classes

• Generic algorithms designed as static methods of class Collections