public class MetodoGenerico {
// definisco un metodo statico generico
public static <E> void printArray(E[] inputArray) {
for (E element : inputArray) {
System.out.printf("%s ", element);
// riga a capo
System.out.println();
}
}
public static void main(String[] args) {
// array di Inteeger, Double, Character
Integer[] integerArray = { 1, 2, 3, 4, 5, 6, 7 };
Double[] doubleArray = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7 };
Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' };
printArray(integerArray);
printArray(doubleArray);
printArray(characterArray);
}
}
Nessun commento:
Posta un commento