简体   繁体   中英

Arrays.asList() implementation in java

In Arrays.java file. The method asList is defined as below.

 @SafeVarargs
    public static <T> List<T> asList(T... arr) {
        return new ArrayList<>(arr);
    }

Here the ArrayList constructor is invoked with an Array.(arr), new ArrayList(arr). But there is no constructor in ArrayList class which accepts Array as argument.Array class is in Collections FrameWork but doesn't implement Collection interface. The constructors in ArrayList are

ArrayList()

ArrayList(Collection<? extends E> c)

ArrayList(int initialCapacity)

can someone explain what is happening there?

它使用Arrays.java文件中存在的私有静态类ArrayList<E>的构造函数ArrayList(E[] array)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM