Author: Nesha Mason

  • Implications for Casting – Generics

    Implications for Casting A non-reifiable type can lose important type information during erasure and the cast may not have the desired effect at runtime. A cast to a non-reifiable type is generally flagged as an unchecked cast warning, and the cast is replaced by a cast to its erasure. Again,…

  • Iterating Over an ArrayList – Collections, Part I: ArrayList

    12.5 Iterating Over an ArrayList<E> Various methods for iterating over collections are discussed in §15.2, p. 791. Here we look at a very common task of iterating over a list to perform some operation on each element of the list. We can use positional access to iterate over a list…

  • Replacing Elements – Collections, Part I: ArrayList

    Replacing Elements The following methods replace elements in a list with new elements. Click here to view code image E set(int index, E element)                         From List<E>interface. Replaces the element at the specified index with the specified element. It returns the previous element at the specified index. The method throws an IndexOutOfBoundsException if…

  • Modifying an ArrayList – Collections, Part I: ArrayList

    12.3 Modifying an ArrayList<E> The ArrayList<E> class provides methods to append, insert, replace, and remove elements from a list. In addition, it has methods to modify the capacity of a list. Adding Elements The various add methods allow elements to be appended at the end of a list and also…

  • Creating Unmodifiable Lists – Collections, Part I: ArrayList

    Creating Unmodifiable Lists Unmodifiable collections are useful to prevent a collection from accidently being modified, as doing so might cause the program to behave incorrectly. Such collections are also stored efficiently, as no bookkeeping is required to support any further modifications and data in the collection can be packed more…

  • Implications for Arrays – Generics

    Implications for Arrays Array store checks are based on the element type being a reifiable type, in order to ensure that subtype covariance between array types is not violated at runtime. In the code below, the element type of the array is String and the array store check at (1)…

  • Limitations and Restrictions on Generic Types – Generics

    11.13 Limitations and Restrictions on Generic Types In this section we take a look at implications and restrictions on generic types for instance tests, casting, arrays, variable arity parameters, exception handling, nested classes, and enum types. Reifiable Types Concrete parameterized types are used by the compiler and then translated by…

  • Lists – Collections, Part I: ArrayList

    12.1 Lists Once an array is created, its length cannot be changed. This inflexibility can be a significant drawback when the amount of data to be stored in an array is not known a priori. In Java, the structures known as lists alleviate this shortcoming. Lists are collections that maintain…

  • Implications for Exception Handling – Generics

    Implications for Exception Handling When an exception is thrown in a try block, it is matched against the parameter of each catch block that is associated with the try block. This test is similar to the instance test, requiring reifiable types. The following restrictions apply, and are illustrated in Example…

  • Recursive Type Bounds Revisited – Generics

    Recursive Type Bounds Revisited The class MonoNode and the interface IMonoLink<E> are declared in Example 11.3, p. 572, and the class BiNode and the interface IBilink<E> are declared in Example 11.5, p. 574. See also Figure 11.1, p. 574. Click here to view code image class MonoNode<E> implements IMonoLink<E> { …