Created
April 9, 2018 15:11
-
-
Save Kashish-pandita-cloudeq/cd46eb5985949a1ec685e752e26c1c69 to your computer and use it in GitHub Desktop.
java generics example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GenericMethodTest { | |
// generic method printArray | |
public static < E > void printArray( E[] inputArray ) { | |
// Display array elements | |
for(E element : inputArray) { | |
System.out.printf("%s ", element); | |
} | |
System.out.println(); | |
} | |
public static void main(String args[]) { | |
// Create arrays of Integer, Double and Character | |
Integer[] intArray = { 1, 2, 3, 4, 5 }; | |
Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 }; | |
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' }; | |
System.out.println("Array integerArray contains:"); | |
printArray(intArray); // pass an Integer array | |
System.out.println("\nArray doubleArray contains:"); | |
printArray(doubleArray); // pass a Double array | |
System.out.println("\nArray characterArray contains:"); | |
printArray(charArray); // pass a Character array | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment