Java ArrayList toArray() 方法的正确使用

来源:互联网 发布:网络通信代维招聘 编辑:程序博客网 时间:2024/05/16 10:35

参考:http://blog.sina.com.cn/s/blog_40585f8d0100058e.html

ArrayList.toArray() 的正确使用方式如下:

public static void main(String arg[]) {        ArrayList<Integer> list = new ArrayList<>();        list.add(0);        list.add(1);        list.add(2);        list.add(3);        /**         * 方法一:         * Type[] l = new Type[<total size>];         * list.toArray(l);         */        Integer[] a = new Integer[list.size()];        list.toArray(a);        /**         * 方法二:         * Type[] l = (Long []) list.toArray(new Type[0]);         */         a = list.toArray(new Integer[0]);        /**         * 方法三:         * a = list.toArray((Type[]) new Object[list.size()]);         */        a = list.toArray((Integer[]) new Object[list.size()]);        for (Integer i : a) {            System.out.println("main:" + i);        }    }
0 0