当我删除Object>
> Array.Object
中的asList(1, 2.89, "three")
时。
List<Object> objs = Arrays.asList(1, 2.89, "three");
它提醒我
类型不匹配:无法转换为List对象&amp; Comparable&amp; Serializable&gt;列出对象&gt;
public class WildcardCopyTest {
public static void main(String[] args) {
List<String> str1 = Arrays.asList("abc", "xyz");
List<String> str2 = Arrays.asList("11");
LowerAndUpperBound.copy(str1, str2);
System.out.println(str1);
List<Object> objs = Arrays.<Object>asList(1, 2.89, "three");
List<Integer> ints = Arrays.asList(100);
LowerAndUpperBound.copy(objs, ints);
System.out.println(objs);
List<Item> items = Arrays.asList(new Item("Java"), new Item("JSPS"));
List<Book2> books = Arrays.asList(new Book2("J2EE", 100.89));
LowerAndUpperBound.copy(items, books);
System.out.println(items);
}
}
我的复制方法:
public static <T> void copy(List<? super T> dest, List<? extends T> src) {
for (int i = 0; i < src.size(); i++) {
dest.set(i, src.get(i));
}