Download CIS 397—Web Design - Missouri State University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Array data structure wikipedia , lookup

Transcript
CIS 270—Application
Development II
Chapter 18-Generics
18.1 Introduction

Generic ________ allow the programmer to specify,
in a single method declaration, a set of related
methods.


Generic ________ allow the programmer to specify,
in a single class declaration, a set of related types.



e.g., one generic method could sort arrays of different
types
e.g., one generic class could be used to instantiate array
objects for an array of Integers, Doubles, Strings, etc.
Compile-time type _______ could be enforced so
that an array stores elements of the same type.
Generics only work with ___________ data types.
2
18.2 Motivation for Generic
Methods

Separate methods, all called printArray, can be
defined to print the elements of any type of array,
such as Integer[], Double[], or Character[].
public static void printArray( Integer[] inputArray )
{
for ( Integer element : inputArray )
System.out.printf( "%s ", element );
System.out.println();
} // see Fig. 18.1 for complete program

It would be convenient to define one printArray
method for any array of generic type E[] and have
the Java compiler determine the data types of
3
different arrays used in an application.
18.3 Generic Methods:
Implementation

Following is an implementation of a generic method:
public static < E > void printArray( E[] inputArray )
{
for ( E element : inputArray )
System.out.printf( "%s ", element );
System.out.println();
}



The E is an arbitrary type ___________ that serves
as a placeholder for an actual argument.
The compiler checks the program to set up method
calls to printArray for different data types.
The compiler also checks for valid operations.
4
18.4 Type Parameters as Return
Types

See Fig. 18.5
public static < T extends Comparable< T > >
T maximum( T x, T y, T z ) // first T is return type
{
T max = x; // assume x is the largest
if ( y.compareTo( max ) > 0 )
max = y; // y is the largest so far
if ( z.compareTo( max ) > 0 )
max = z; // z is the largest
return max; // returns the largest object
} // end method maximum

If primitives are sent to maximum, the compiler
_________ them as objects (and compareTo is valid).
5
18.5 Overloading Generic
Methods






A class can have two different generic methods with
the same name, but different method parameters.
A generic method can be overloaded by non-generic
methods of the same name and number of
parameters.
The compiler first looks for an exact match of method
names and argument types.
If an exact match isn’t found, the compiler looks for
__________.
_________ is the process whereby the compiler
replaces type parameters with actual types.
Type parameters can have an upper _________. 6
18.6 Generic Classes



A generic class is used to describe something in a
type-independent manner.
You can specify the actual types that should be used
in place of the class’s ______ parameter(s).
For example, one generic Stack class could be used
to create many Stack classes.



A stack is a data structure (list of elements) with a ______
(last in, first out) organization.
An element is pushed onto the stack and popped off of the
stack (like dishes in a stack of dishes).
Fig. 18.7 presents a generic Stack class declaration
that uses an array of type E to implement the Stack.
7
18.7 Raw Types

If a class is instantiated without specifying the type
argument, Java will use type Object.




This ensures ___________ compatibility with earlier
versions of Java.
A raw type Stack variable can be assigned a
generic stack:


Stack objectStack = new Stack( 5 ); instead of
Stack doubleStack = new Stack< Double >( 5 );
Stack rawTypeStack = new Stack< Double >( 5 );
A generic type Stack variable can be assigned a
raw type, but it is ____________:

Stack< Integer > integerStack = new Stack( 10 ); 8