Integer is a subtype of Number
Double is a subtype of Number
ArrayList<E> is a subtype of List<E>
List<E> is a subtype of Collection<E>
Collection<E> is a subtype of Iterable<E>
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
public static void main(String args[]) { List<Number> nums = new ArrayList<Number>(); List<Integer> ints = Arrays.asList(1, 2); List<Double> dbls = Arrays.asList(2.78, 3.14); nums.addAll(ints); nums.addAll(dbls); for(Number d: nums) System.out.println(d); }
package cn.galc.test; import java.util.*; class Collections{ public static <T> List<T> toList(T[] arr) { List<T> list = new ArrayList<T>(); for (T t: arr) { list.add(t); } return list; } public static <T> void copy(List<? super T> dst, List<? extends T> src) { for (int i = 0; i < src.size(); i++) { dst.set(i, src.get(i)); } } } public class Test { public static void main(String args[]) { List<Object> objs = Arrays.<Object>asList(2, 3.14, "four"); List<Integer> ints = Arrays.asList(5, 6); Collections.copy(objs, ints); for(Object o: objs) System.out.println(o); } }
时间: 2024-11-05 21:48:29