Java Reference
In-Depth Information
developing among programmers for the class ArrayList . There are some differences
between the classes Vector and ArrayList , but the differences involve material we
have not covered. If you encounter the class Vector in somebody's code, chances are
the class Vector could be replaced by the class ArrayList , which would require at
most cosmetic changes in the code. 1
Parameterized Classes and Generics
The class ArrayList is a parameterized class . It has a parameter, which we have
been denoting Base_Type , that can be replaced by any reference type to obtain a class
for ArrayList s with the specified base type. ArrayList is just a class that somebody
defined (and placed in the standard Java library package java.util ), so you should
also be able to define these kinds of classes. Starting with version 5.0, Java allows
class definitions with parameters for types. These classes that have type parameters
are called parameterized class or generic definitions or, more simply, generics . You
already know how to use classes with a type parameter, because we have been using the
parameterized class ArrayList . In Section 14.2, we will show you how to write your
own parameterized classes.
parameterized
class
generics
PITFALL: Nonparameterized ArrayList and Vector Classes
The ArrayList and Vector classes we discussed in this section have a type parameter
for the base type. There are also ArrayList and Vector classes with no parameter
for the base type. (They have base type Object .) These ArrayList and Vector
classes without type parameters are left over from earlier versions of Java. When
checking details in the Java documentation, be sure you get the documentation for the
ArrayList and Vector classes that have a type parameter. Using notation we introduce
in Section 14.2, the versions with type parameters are usually written as ArrayList<E>
and Vector<E> or as ArrayList<T> and Vector<T> in the Java documentation.
14.2
Generics
You can have this dish prepared with any type of meat or fish.
Entry on a restaurant menu
Starting with version 5.0, Java allows class definitions that contain a parameter (or
parameters) for a type (or types). In this section, we teach you how to write class
definitions that contain a type parameter.
1 The biggest difference between the Vector and ArrayList classes is that Vectors are synchronized
whereas ArrayLists are not. However, synchronization is a topic that we do not cover and is not
relevant to the kinds of programming we are doing.
 
 
Search WWH ::




Custom Search