import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class asdas {
public static void main(String[] args) {
List list = Arrays.asList("jack","lisa","tom");
List lists1 = Arrays.asList(new String[]{"jack","lisa","tom"});
List list2 = new ArrayList();
boolean addAll = Collections.addAll(list2, new String[]{"jack","lisa","tom"});
List list3 = new ArrayList();
boolean addAll2 = Collections.addAll(list3, "jack","lisa","tom");
System.out.println("list:"+list);
System.out.println("list1:"+lists1);
System.out.println("list2:"+list2);
System.out.println("list3:"+list3);
}
}
運作結果:
list:[jack, lisa, tom]
list1:[jack, lisa, tom]
list2:[jack, lisa, tom]
list3:[jack, lisa, tom]
注意:
使用Arrays.asList這種方法生成的list是不能使用list.add remove等方法的,因為Arrays.asList()方法傳回的是一個和ArrayList同名的自定義内部類,該内部類和ArrayList一樣實作了
private static class ArrayList extends AbstractList implements RandomAccess, java.io.Serializable ,,,但是它沒有自己重寫add ,和remove,而是使用父類AbstractList裡面的方法,導緻使用add,remove時抛出父類中的異常,大家可以檢視源碼,是以說這是一個可讀的List。不可對其操作。