首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java List与数组其间的转换

2013-01-01 
Java List与数组之间的转换1 数组转换为List调用Arrays类的静态方法asList。asListpublic static T List

Java List与数组之间的转换

1 数组转换为List

调用Arrays类的静态方法asList。

asList
public static <T> List<T> asList(T... a)
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<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); 

Parameters:a - the array by which the list will be backedReturns:a list view of the specified array

API中提供了一种使用的方法。更为常用的示例代码:

List<String> list = new ArrayList<String>();list.add("str1");list.add("str2");int size = list.size();String[] arr = (String[])list.toArray(new String[size]);//使用了第二种接口,返回值和参数均为结果

热点排行