Array.asList()
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 Collection.toArray . The returned list is serializable and implements RandomAccess
.
This method also provides a convenient way to create a fixed-size list initialized to contain several elements:
List stooges = Arrays.asList("Larry", "Moe", "Curly");
Array.asList的输出可以当作一个List,但在这种情况下,其底层表示的是数组,因此不能调整尺寸(定长的).如果试图利用add()和delete()方法在这种列表中添加或删除元素,编译器就会报错.
Collection.addAll()
Adds all of the elements in the specified collection to this collection (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this collection, and this collection is nonempty.)
该方法只如此接受另一个Collection对象作为参数(参数列表不可变,灵活性不高).注意与Collections.addAll()比较.
Collections.addAll()(首选方式)
Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)) , but this method is likely to run significantly faster under most implementations.
When elements are specified individually, this method provides a convenient way to add a few elements to an existing collection:
Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");