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);
        }
    }
  • 相关阅读:
    简单手风琴特效、轮播
    MVC
    文字自动下拉隐藏显示
    jQuery基础 DOM操作
    JQuery基础
    SQL 中的常用函数及使用
    数据库中的T-sql语句 条件修改 高级查询
    2017-03-09 数据库的基本东西
    C#中的冒泡排序
    C#中的数组
  • 原文地址:https://www.cnblogs.com/Ring1981/p/4471870.html
Copyright © 2011-2022 走看看