Source of GenericMethodTest.java


  1: // Fig. 18.3: GenericMethodTest.java
  2: // Using generic methods to print array of different types.
  3: 
  4: public class GenericMethodTest 
  5: {
  6:    // generic method printArray
  7:    public static < E > void printArray( E[] inputArray )
  8:    {
  9:       // display array elements
 10:       for ( E element : inputArray )
 11:          System.out.printf( "%s ", element );
 12: 
 13:       System.out.println();
 14:    } // end method printArray
 15: 
 16:    public static void main( String args[] ) 
 17:    {
 18:       // create arrays of Integer, Double and Character
 19:       Integer[] integerArray = { 1, 2, 3, 4, 5, 6 };
 20:       Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
 21:       Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' };
 22: 
 23:       System.out.println( "Array integerArray contains:" );
 24:       printArray( integerArray ); // pass an Integer array 
 25:       System.out.println( "\nArray doubleArray contains:" );
 26:       printArray( doubleArray ); // pass a Double array
 27:       System.out.println( "\nArray characterArray contains:" );
 28:       printArray( characterArray ); // pass a Character array
 29:    } // end main
 30: } // end class GenericMethodTest
 31: 
 32: /**************************************************************************
 33:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 34:  * Pearson Education, Inc. All Rights Reserved.                           *
 35:  *                                                                        *
 36:  * DISCLAIMER: The authors and publisher of this book have used their     *
 37:  * best efforts in preparing the book. These efforts include the          *
 38:  * development, research, and testing of the theories and programs        *
 39:  * to determine their effectiveness. The authors and publisher make       *
 40:  * no warranty of any kind, expressed or implied, with regard to these    *
 41:  * programs or to the documentation contained in these books. The authors *
 42:  * and publisher shall not be liable in any event for incidental or       *
 43:  * consequential damages in connection with, or arising out of, the       *
 44:  * furnishing, performance, or use of these programs.                     *
 45:  *************************************************************************/