-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericMethodDemo.java
More file actions
29 lines (23 loc) · 976 Bytes
/
GenericMethodDemo.java
File metadata and controls
29 lines (23 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class GenericMethodDemo {
public static void main(String[] args) {
String[] strArray = {"11", "22", "33"};
Integer[] intArray = {1, 2, 3};
Character[] charArray = {'H', 'E', 'L', 'L', 'O'};
Double[] doubleArray = {1.1, 2.2, 3.3};
System.out.println("\n字符串数组元素为:");
printArray(strArray); // 传递一个字符串数组
System.out.println("\n整型数组元素为:");
printArray(intArray); // 传递一个整型数组
System.out.println("\n双精度型数组元素为:");
printArray(doubleArray); // 传递一个双精度型数组
System.out.println("\n字符型数组元素为:");
printArray(charArray); // 传递一个字符型数组
}
public static <T> void printArray(T[] inputArray) {
for (T element : inputArray
) {
System.out.printf("%s ", element);
}
System.out.println();
}
}