版权声明:本文为博主原创文章,转载请说明出处 https://blog.csdn.net/u010002184/article/details/81328227
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<String> list = Arrays.asList(arr);
// List<String> list = Arrays.asList("","1","2","3");//正确
// List<String> list = Arrays.asList();//正确
List<String> 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}.
*
* <p>This method also provides a convenient way to create a fixed-size
* list initialized to contain several elements:
* <pre>
* List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
* </pre>
*
* @param <T> 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 <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
T... a , 传入数组或者多个字符串均可