-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeneric.java
More file actions
47 lines (35 loc) · 949 Bytes
/
Generic.java
File metadata and controls
47 lines (35 loc) · 949 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package generic;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
/**
* Created by Edwin Xu on 4/28/2020 1:33 PM
*/
class MyGeneric<T>{
private T id;
public MyGeneric(T id){
this.id = id;
}
public void print(){
System.out.println(id.toString());
}
}
public class Generic {
public static void main(String[] args) {
ArrayList<Integer> a = new ArrayList<>();
a.add(1);
//Generic Methods:
int i = Collections.binarySearch(a,1);
//返回的是index
System.out.println(i);
System.out.println(a.get(i));
//Generic Interface
Iterator<Integer> iterator = a.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
MyGeneric<String> myGeneric = new MyGeneric<>("EdwinXu");
myGeneric.print();
}
}