zoukankan      html  css  js  c++  java
  • Generics and Collection (2)

    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);
        }
    }
  • 相关阅读:
    java对象的四种引用
    linux安装python3
    ORACLE配置重做日志文件
    oracle添加控制文件,ORA-00214: 错误
    oracle new 和old 关键字
    with open
    json库
    requests
    urllib模块
    python读取txt天气数据并使用matplotlib模块绘图
  • 原文地址:https://www.cnblogs.com/Ring1981/p/4471870.html
Copyright © 2011-2022 走看看