Printing array elements using generic method printArray. - Java Object Oriented Design

Java examples for Object Oriented Design:Generic Method

Description

Printing array elements using generic method printArray.

Demo Code

public class Main 
{
   public static void main(String[] args) 
   {/*from   w ww. ja v  a 2  s  .co m*/
      // create arrays of Integer, Double and Character
      Integer[] integerArray = {1, 2, 3, 4, 5, 6};
      Double[] doubleArray = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
      Character[] characterArray = {'H', 'E', 'L', 'L', 'O'};

      System.out.print("Array integerArray contains:%n");
      printArray(integerArray); // pass an Integer array 
      System.out.printf("%nArray doubleArray contains:%n");
      printArray(doubleArray); // pass a Double array
      System.out.printf("%nArray characterArray contains:%n");
      printArray(characterArray); // pass a Character array
   } 

   // generic method printArray
   public static <T> void printArray(T[] inputArray)
   {
      // display array elements
      for (T element : inputArray)
         System.out.printf("%s ", element);

      System.out.println();
   }
}

Result


Related Tutorials