Files
effective-Java/ch05泛型/Generics/src/main/java/com/jueee/item26/Item26Example02.java
2019-11-21 19:59:07 +08:00

24 lines
659 B
Java

package com.jueee.item26;
import java.util.ArrayList;
import java.util.List;
public class Item26Example02 {
// Fails at runtime - unsafeAdd method uses a raw type (List)!
public static void main(String[] args) {
List<String> strings = new ArrayList<>();
// The method unsafeAdd(List<Object>, Object) in the type Item26Example02 is not applicable for the arguments (List<String>, Integer)
// unsafeAdd(strings, Integer.valueOf(42));
String s = strings.get(0);
System.out.println(s);
}
private static void unsafeAdd(List<Object> list, Object o) {
list.add(o);
}
}