ch8

Cards (34)

  • Java (5.0+) allows class definitions with parameters for types, are called as parameterized classes, generic definitions, or simply generics
  • Parameterized classes

    Class definitions with parameters for types
  • Generic definitions

    Class definitions with parameters for types
  • Generics
    Class definitions with parameters for types
  • The class ArrayList is a parameterized class. It has a parameter, denoted by Base_Type, that can be replaced by any reference type to obtain a class for ArrayLists with the specified base type
  • A class definition with a type parameter is stored in a file, and compiled and used just like any other class. However, the class type plugged in for the type parameter must be specified before it can be used in a program. Doing this is said to instantiate the generic class
  • Type parameter

    Included in angular brackets after the class name in the class definition heading
  • Any non-keyword identifier can be used for the type parameter, but by convention, the parameter starts with an uppercase letter
  • The type parameter can be used like other types in class definition
  • Type parameter is not used in heading of default constructor
  • Methods can use the type parameter without angular brackets
  • Instantiating a generic class
    Using angular brackets
  • The type parameter cannot be a primitive type such as int, double, or char
  • We cannot make an array of objects from a parameterized class
  • A generic class cannot extend Exception, Error, Throwable (or its descendants)
  • A generic class can extend an ordinary class or a generic class
  • Many pitfalls can be encountered when using type parameters. Compiling with -Xlint option (to enable all warnings) will provide more informative diagnostics of any problems in the code
  • ArrayList is a class in the standard Java libraries
  • ArrayList
    Serves the same purpose as an array, except that an ArrayList can change length while the program is running
  • The class ArrayList is implemented using an array as a private instance variable
  • When the hidden array is full, a new larger hidden array is created and the data is transferred to this new array
  • Disadvantages of ArrayList Class
    • An ArrayList is less efficient than an array
    • It does not have the convenient square bracket notation
    • The base type of an ArrayList must be a class type (or other reference type), it cannot be a primitive type
  • The definition of class ArrayList is in the package java.util, and any code that uses it must import it, normally at the start of the file
  • Creating an ArrayList
    ArrayList<BaseType> aList = new ArrayList<BaseType>(size);
  • Specifying an initial capacity does not limit the size to which an ArrayList can eventually grow
  • add method
    Used to set an element for the first time in an ArrayList
  • set method
    Used to replace any existing element
  • get method
    Used to access the value of any existing element
  • size method
    Returns the number of elements stored in the ArrayList
  • When looking at the methods available in the ArrayList class, there appears to be some inconsistency in the parameter types
  • Invoking clone on an ArrayList object produces a shallow copy, not a deep copy
  • The Java standard libraries have a class named Vector that behaves almost exactly the same as the class ArrayList
  • The ArrayList class is newer, and is becoming the preferred class over Vector
  • There are also ArrayList and Vector classes with no parameter whose base type is Object. These classes are left over from earlier versions of Java