ListOps.java
// This code example is from the following source: // // Book Title: Programming with Objects, A Comparative Presentation // of Object-Oriented Programming with C++ and Java // // Chapter: Chapter 5 ---- Using the Container Classes // // Section: Section 5.2.1 ---- List // // The links to the rest of the code in this book are at // // http://programming-with-objects.com/pwocode.html // // For further information regarding the book, please visit // // http://programming-with-objects.com // //ListOps.java import java.util.*; class ListOps { public static void main( String[] args ) { List animals = new ArrayList(); //(A) animals.add( "cheetah" ); //(B) animals.add( "lion" ); animals.add( "cat" ); animals.add( "fox" ); animals.add( "cat" ); //duplicate cat //(C) System.out.println( animals ); //cheetah, lion, cat, fox, //cat animals.remove( "lion" ); //(D) System.out.println( animals ); //cheetah, cat, fox, cat animals.add( 0, "lion" ); //(E) System.out.println( animals ); //lion, cheetah, cat, fox, //cat animals.add( 3, "racoon" ); //(F) System.out.println( animals ); //lion, cheetah, cat, //racoon, fox, cat animals.remove(3); //(G) System.out.println( animals ); //lion, cheetah, cat, //fox, cat Collections.sort( animals ); //(H) System.out.println( animals ); //cat, cat, cheetah, //fox, lion List pets = new LinkedList(); //(I) pets.add( "cat" ); //(J) pets.add( "dog" ); pets.add( "bird" ); System.out.println( pets ); //cat, dog, bird animals.addAll( 3, pets ); //(K) System.out.println( animals ); //cat, cat, cheetah, //cat, dog, bird, fox, //lion ListIterator iter = animals.listIterator(); //(L) while ( iter.hasNext() ) { //(M) System.out.println( iter.next() ); //(N) } } }
The generics mechanism in the language provides compile-time (static) type checking, but it is possible to defeat this mechanism with unchecked casts. Usually this is not a problem, as the compiler issues warnings on all such unchecked operations. There are, however, times when static type checking alone is not sufficient. For example, suppose a collection is passed to a third-party library and it is imperative that the library code not corrupt the collection by inserting an element of the wrong type.
Another use of dynamically typesafe views is debugging. Suppose a program fails with a ClassCastException, indicating that an incorrectly typed element was put into a parameterized collection. Unfortunately, the exception can occur at any time after the erroneous element is inserted, so it typically provides little or no information as to the real source of the problem. If the problem is reproducible, one can quickly determine its source by temporarily modifying the program to wrap the collection with a dynamically typesafe view. For example, this declaration:
Collection<String> c = new HashSet<String>();may be replaced temporarily by this one:
Collection<String> c = Collections.checkedCollection( new HashSet<String>(), String.class);Running the program again will cause it to fail at the point where an incorrectly typed element is inserted into the collection, clearly identifying the source of the problem. Once the problem is fixed, the modified declaration may be reverted back to the original.
Maintained by John Loomis, updated Sun Jan 07 14:24:34 2007