//提供两种使用情况,第二种情况定义前者是后者的子类-类型通配方式
package ming;
import java.util.ArrayList;
import java.util.Collection;
public class GenericMethodTest {
static void fromArraytoCollection(T[] a, Collection c) {
for (T o : a) {
c.add(o);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//T stand for Object
Object[] oa = new Object[100];
Collection co = new ArrayList();
fromArraytoCollection(oa,co);
//T stand for Number
Integer[] ia = new Integer[100];
Float[] fa = new Float[100];
Collection cn = new ArrayList();
fromArraytoCollection(ia,cn);
fromArraytoCollection(fa,cn);
}
}
package ming;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class GenericMethodTest {
static void fromArraytoCollection(Collection extends T> from,
Collection to) {
for (T ele : from) {
to.add(ele);
}
}
public static void main(String[] args) {
List ao = new ArrayList();
List as = new ArrayList();
// string is subclass of object
fromArraytoCollection(as, ao);
}
}