java,Arrays.asList用法

import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
//        String[] arr = new String[]{"0.56", "1.23", "0", "12.2", "78", "123.6"};//正确
//        List list = Arrays.asList(arr);
//        List list = Arrays.asList("","1","2","3");//正确
//        List list = Arrays.asList();//正确
        List list = Arrays.asList(null);//错误,NullPointerException
        System.out.println(list);
    }

}

源码(JDK1.8):

    /**
     * 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}.
     *
     * 

This method also provides a convenient way to create a fixed-size * list initialized to contain several elements: *

     *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
     * 
* * @param the class of the objects in the array * @param a the array by which the list will be backed * @return a list view of the specified array */ @SafeVarargs @SuppressWarnings("varargs") public static List asList(T... a) { return new ArrayList<>(a); }

T... a , 传入数组或者多个字符串均可

你可能感兴趣的:(Java)