首页 > 其他 > 详细

Generics and Collection (2)

时间:2015-05-02 13:42:26      阅读:120      评论:0      收藏:0      [点我收藏+]

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);
    }
}

 

Generics and Collection (2)

原文:http://www.cnblogs.com/Ring1981/p/4471870.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!