Close
Register
Close Window

Software Design and Data Structures

Chapter 9 Lists and Generics

Show Source |    | About   «  9.1. Lists   ::   Contents   ::   9.3. Lab 9 Single Link  »

9.2. More on Generics

9.2.1. Objectives

Upon completion of this module, students will be able to:

  • Determine when a generic needs to be bounded

  • Write and use methods and classes that use bounded generic parameters

  • Write and use bounded generic methods

  • Become familiar with syntax for wildcards

9.2.1.1. Suggested Reading:

Java Interlude 8 Generics Once Again from Data Structures and Abstractions with Java, 4th edition by Frank M. Carrano and Timothy Henry

9.2.2. Interactive: More on Generics

Follow Along, Practice and Explore

Download to run and explore the corresponding project from the video on your own in eclipse. The project CS2-Support is required for the sample project above. It is also used in your course projects. To download the CS2-Support you must first complete the configuration steps for your first lab. You will then be able to download it via eclipse using the blue down arrow icon or using the Project Menu and selecting “Download Assignment…”

exGenerics.zip
MoreOnGenerics.pdf

9.2.3. Interactive: Reflecting on Generics

9.2.4. Sample Declarations of Generic Methods Explained

The format for declaring a Generic method is as follows:

methodModifiers <genericParameters> returnType methodName(methodParameters)

Note the use of the generic parameters placed inside the angle brackets.

Example 1

Below is one example of how you may declare a Generic method.

public static <T> void sort(T[] items, Comparator<? super T> comp)

The T following the static keyword and enclosed within the angle brackets represents the generic parameter for the sort method. The T should also appear in the method parameter list.

The second method parameter Comparator<? super T> comp is our way of specifying that comp must be an object that implements the Comparator interface for type T or for a superclass of type T

We use this approach to specify restrictions, for example, you can define a class that implements Comparator<Number> and use it to sort an array of Integer objects or an array of Double objects

Example 2

Below is another example Generic method declaration.

public static <T extends Comparable<T>> void sort(List<T> list)

The use of <T extends Comparable<T>> specifies that the generic parameter T must implement the interface Comparable<T>. The method parameter list (the object being sorted) is of type List<T>.

9.2.5. Checkpoint 1

9.2.6. Interactive: Bounded Wildcard Examples

Follow Along, Practice and Explore

Download to run and explore the corresponding project from the video on your own in eclipse. The project CS2-Support is required for the sample project above. It is also used in your course projects. To download the CS2-Support you must first complete the configuration steps for your first lab. You will then be able to download it via eclipse using the blue down arrow icon or using the Project Menu and selecting “Download Assignment…”

exGenerics.zip

9.2.7. Programming Practice: Generics 1

   «  9.1. Lists   ::   Contents   ::   9.3. Lab 9 Single Link  »

nsf
Close Window