import java.util.Arrays;
import java.util.List;
public class AddingGroup {
public static void main(String args[]){
List<Integer> list = Arrays.asList(16,17,18,19,20);
list.set(1,99);
list.add(21);
}
}
import java.util.List;
public class AddingGroup {
public static void main(String args[]){
List<Integer> list = Arrays.asList(16,17,18,19,20);
list.set(1,99);
list.add(21);
}
}
public static <T> List<T> asList(T... a)返回一个受指定数组支持的固定大小的列表。
因此,不支持添加操作,修改操作是可以的
private final E[] a;ArrayList(E[] array) {
if (array==null)
throw new NullPointerException();
a = array;
}其add方法调用的是AbstractList中的:
/**
* {@inheritDoc}
*
* <p>This implementation always throws an
* {@code UnsupportedOperationException}.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
刚好看到,底层表示的是数组,因此不能调尺寸,如果去调将在运行时获得"Unsupported Operation"错误。