Source of WildcardTest.java


  1: // Fig. 18.15: WildcardTest.java
  2: // Wildcard test program.
  3: import java.util.ArrayList;
  4: 
  5: public class WildcardTest 
  6: {
  7:    public static void main( String args[] ) 
  8:    {
  9:       // create, initialize and output ArrayList of Integers, then 
 10:       // display total of the elements 
 11:       Integer[] integers = { 1, 2, 3, 4, 5 };
 12:       ArrayList< Integer > integerList = new ArrayList< Integer >();
 13: 
 14:       // insert elements in integerList
 15:       for ( Integer element : integers )
 16:          integerList.add( element );
 17: 
 18:       System.out.printf( "integerList contains: %s\n", integerList );
 19:       System.out.printf( "Total of the elements in integerList: %.0f\n\n",
 20:          sum( integerList ) );
 21: 
 22:       // create, initialize and output ArrayList of Doubles, then 
 23:       // display total of the elements 
 24:       Double[] doubles = { 1.1, 3.3, 5.5 };
 25:       ArrayList< Double > doubleList = new ArrayList< Double >();
 26: 
 27:       // insert elements in doubleList
 28:       for ( Double element : doubles )
 29:          doubleList.add( element );
 30: 
 31:       System.out.printf( "doubleList contains: %s\n", doubleList );
 32:       System.out.printf( "Total of the elements in doubleList: %.1f\n\n", 
 33:          sum( doubleList ) );
 34: 
 35:       // create, initialize and output ArrayList of Numbers containing 
 36:       // both Integers and Doubles, then display total of the elements 
 37:       Number[] numbers = { 1, 2.4, 3, 4.1 }; // Integers and Doubles
 38:       ArrayList< Number > numberList = new ArrayList< Number >();
 39: 
 40:       // insert elements in numberList
 41:       for ( Number element : numbers )
 42:          numberList.add( element );
 43: 
 44:       System.out.printf( "numberList contains: %s\n", numberList );
 45:       System.out.printf( "Total of the elements in numberList: %.1f\n", 
 46:          sum( numberList ) );
 47:    } // end main
 48: 
 49:    // calculate total of stack elements
 50:    public static double sum( ArrayList< ? extends Number > list )
 51:    {
 52:       double total = 0; // initialize total
 53: 
 54:       // calculate sum
 55:       for ( Number element : list )
 56:          total += element.doubleValue();
 57: 
 58:       return total;
 59:    } // end method sum
 60: } // end class WildcardTest
 61: 
 62: /**************************************************************************
 63:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 64:  * Pearson Education, Inc. All Rights Reserved.                           *
 65:  *                                                                        *
 66:  * DISCLAIMER: The authors and publisher of this book have used their     *
 67:  * best efforts in preparing the book. These efforts include the          *
 68:  * development, research, and testing of the theories and programs        *
 69:  * to determine their effectiveness. The authors and publisher make       *
 70:  * no warranty of any kind, expressed or implied, with regard to these    *
 71:  * programs or to the documentation contained in these books. The authors *
 72:  * and publisher shall not be liable in any event for incidental or       *
 73:  * consequential damages in connection with, or arising out of, the       *
 74:  * furnishing, performance, or use of these programs.                     *
 75:  *************************************************************************/