当传入的是基本类型时,会把传过来的数组作为list的第一个元素:
public class ArraysasList {
public static void main(String[] args) {
int[] a = {1,2,3,4,8,10,22,12,214,23};
String[] b = {"a","b","c"};
Integer[] c = {1,2,3,4,8,10,22,12,214,23};
System.out.println(a);
System.out.println(Arrays.asList(b));
System.out.println(Arrays.asList(c));
}
}
输出结果:
[I@179935d
[a, b, c]
[1, 2, 3, 4, 8, 10, 22, 12, 214, 23]
Arrays第二个问题,在添加或者删除的时候会报错:
public class ArraysasList {
public static void main(String[] args) {
int[] a = {1,2,3,4,8,10,22,12,214,23};
String[] b = {"a","b","c"};
Integer[] c = {1,2,3,4,8,10,22,12,214,23};
System.out.println(a);
System.out.println(Arrays.asList(b));
System.out.println(Arrays.asList(c));
List<String> bList = Arrays.asList(b);
bList.add("d");
System.out.println(bList);
}
}
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at ArraysasList.main(ArraysasList.java:16)
源码中写到:
public void add( int index, E element) { |
07 |
throw new UnsupportedOperationException(); |
10 |
public E remove( int index) { |
11 |
throw new UnsupportedOperationException(); |