GenericDemo.java

 

Download GenericDemo.java source file

// Simple demo of creating and using a generic method.
// Written 2005 by Wayne Pollock, Tampa Florida USA.

class ArrayKit
{
   public static <T> T getMiddleElement ( T[] array )
   {
      T middleElement = array[array.length/2];
      return middleElement; 
   }
}

public class GenericDemo
{
   public static void main ( String [] args )
   {
      String[] names = { "Abe", "Bob", "Chuck" };
      String s = ArrayKit.getMiddleElement(names);
      Integer [] ages = { 18, 29, 43 };
      int i = ArrayKit.getMiddleElement( ages );
      System.out.println( "name: " + s + ", age: " + i );
   }
}