-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
49 lines (40 loc) · 1.45 KB
/
Copy pathMain.java
File metadata and controls
49 lines (40 loc) · 1.45 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
package main;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Object> list1 = new ArrayList<>(10);
list1.add(10.10);
list1.add("foo");
printList(list1);
List<?> list2 = new ArrayList<>(10);
// what an error message from javac
// Error:(18, 14) java: no suitable method found for add(int)
// method java.util.Collection.add(capture#1 of ?) is not applicable
// (argument mismatch; int cannot be converted to capture#1 of ?)
// method java.util.List.add(capture#1 of ?) is not applicable
// (argument mismatch; int cannot be converted to capture#1 of ?)
// thankfully Intellij has a better error message
// add(capture<?>) in List cannot be applied to (int)
// type-error
// list2.add(10);
printList(list2);
int value = 20;
List<Object> list3 = new ArrayList<>(10);
list3.add(10);
list3.add(value);
list3.add(10.10);
list3.add("foo");
list3.add(new Integer[] { 4, 5, 6});
list3.add(new int[] { 1, 2, 3}); // works as well
printList(list3);
}
public static void printList(List<?> list) {
for (Object elem: list) {
System.out.print(elem + " ");
}
System.out.println();
}
public static <T extends Object> void printValue(T element) {
}
}