-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGElement.java
More file actions
61 lines (48 loc) · 1.44 KB
/
GElement.java
File metadata and controls
61 lines (48 loc) · 1.44 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package example.GenericExample;
/**
* 泛型方法 Element
*
* Reference:
* - https://www.runoob.com/java/java-generics.html
*/
public class GElement {
public static void main(String[] args) {
Integer[] intArray = {1, 2, 3, 4, 5, 6};
Double[] doubleArray = {1.2, 2.4, 3.7, 4.9, 5.0};
Character[] charArray = {'H', 'G', 'O', 'E', 'F', 'K'};
printArray(intArray);
printArray(doubleArray);
printArray(charArray);
System.out.println("the max number: " + maximum(3, 4, 5));
Box<Integer> boxInteger = new Box<Integer>(10);
Box<String> boxString = new Box<String>("New");
System.out.println("boxInteger: " + boxInteger.get());
System.out.println("boxString: " + boxString.get());
}
public static <E> void printArray(E[] inputArray) {
// print all elements in the array.
for (E element : inputArray) {
System.out.println(element);
}
}
public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
// compare three value and return max value.
T max = x;
if (y.compareTo(max) > 0) {
max = y;
}
if (z.compareTo(max) > 0) {
max = z;
}
return max;
}
}
class Box<T> {
private T t;
public Box(T t) {
this.t = t;
}
public T get() {
return t;
}
}