为容器添加一组元素的方法

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");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值