码迷,mamicode.com
首页 > 其他好文 > 详细

Arrays.asList的哪点事

时间:2015-11-24 09:47:14      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class ArraysAsListDemo {
    public static void main(String[] args) {
        List<String> source1 = new ArrayList<String>(Arrays.asList("1", "2", "3"));
        ArrayList<String> source2 = new ArrayList<String>(Arrays.asList("5", "62", "3"));
        source2.removeAll(source1);
        System.out.println(source2);
//        Arrays.asList("1", "2", "3").remove("1");//会报错。因为Arrays$ArrayList中没有实现remove方法

        System.out.println(source1.containsAll(source2));
        HashSet<String> total = new HashSet<String>();
        total.addAll(source1);
        total.addAll(source2);
        System.out.println(total.size() > (source1.size() + source2.size()));
        System.out.println(total.size() < (source1.size() + source2.size()));
    }

}

 



    // Misc

    /**
     * Returns a fixed-size list backed by the specified array.  (Changes to
     * the returned list "write through" to the array.)  This method acts
     * as bridge between array-based and collection-based APIs, in
     * combination with {@link Collection#toArray}.  The returned list is
     * serializable and implements {@link RandomAccess}.
     *
     * <p>This method also provides a convenient way to create a fixed-size
     * list initialized to contain several elements:
     * <pre>
     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
     * </pre>
     *
     * @param a the array by which the list will be backed
     * @return a list view of the specified array
     */
    public static <T> List<T> asList(T... a) {
    return new ArrayList<T>(a);
    }

    /**
     * @serial include
     */
    private static class ArrayList<E> extends AbstractList<E>
    implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
    private final E[] a;

    ArrayList(E[] array) {
            if (array==null)
                throw new NullPointerException();
        a = array;
    }

 





Arrays.asList的哪点事

标签:

原文地址:http://www.cnblogs.com/softidea/p/4990463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!