Java中List与Array的转换

文章作者:Tyan
博客:noahsnail.com

    在Java项目开发过程中,集合之间的互相转换是非常常见的,其中两个比较典型的转换是List和Array之间的转换,本文主要介绍这二者之间的转换、其中存在的一些问题以及解决方案,本文JDK版本为1.8。

1.Array To List

Array To List在Java中的方法是Arrays.asList()方法,这是在Java开发中常用的方法,在一般情况下使用这个方法将Array转为List都没问题,但要对转换后的List进行修改时会出现下面的异常:

1
2
3
4
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at com.liu.test.Test.main(Test.java:17)

测试代码为:

1
2
3
4
5
6
7
public class Test {
public static void main(String[] args) {
String[] str = {"134", "test", "list"};
List<String> list = Arrays.asList(str);
list.add("");
}
}

为什么会出现这个错误呢?看一下Arrays.asList()方法的源码及介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 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 {@link Collection#toArray}. The returned list is
* serializable and implements {@link RandomAccess}.
*
* <p>This method also provides a convenient way to create a fixed-size
* list initialized to contain several elements:
* <pre>
* List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
* </pre>
*
* @param <T> the class of the objects in the array
* @param a the array by which the list will be backed
* @return a list view of the specified array
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}

注释中第一句话就说了,Arrays.asList()方法返回的是一个固定大小的List,如何来使返回的List可添加或删除元素呢?
方案一:

1
List<String> list = new ArrayList<String>(Arrays.asList(str));

方案二:使用Google Guava,需要import Lists:

1
List<String> list = Lists.newArrayList(str);

方案三:使用Apache Commons Collections,需要import CollectionUtils:

1
2
List<String> list = new ArrayList<String>();
CollectionUtils.addAll(list, str);

上面三种方案根据项目需要自己选择即可。

2.List To Array

List To Array在Java中的方法是list.toArray()方法,但这个方法有个问题是返回的数组对象为Object[],直接用String[]去强制转换会报语法错误,直接点击toArray()方法去查看源码,当然也可以直接看官方API文档:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Returns an array containing all of the elements in this list in proper
* sequence (from first to last element).
*
* <p>The returned array will be "safe" in that no references to it are
* maintained by this list. (In other words, this method must
* allocate a new array even if this list is backed by an array).
* The caller is thus free to modify the returned array.
*
* <p>This method acts as bridge between array-based and collection-based
* APIs.
*
* @return an array containing all of the elements in this list in proper
* sequence
* @see Arrays#asList(Object[])
*/
Object[] toArray();
/**
* Returns an array containing all of the elements in this list in
* proper sequence (from first to last element); the runtime type of
* the returned array is that of the specified array. If the list fits
* in the specified array, it is returned therein. Otherwise, a new
* array is allocated with the runtime type of the specified array and
* the size of this list.
*
* <p>If the list fits in the specified array with room to spare (i.e.,
* the array has more elements than the list), the element in the array
* immediately following the end of the list is set to <tt>null</tt>.
* (This is useful in determining the length of the list <i>only</i> if
* the caller knows that the list does not contain any null elements.)
*
* <p>Like the {@link #toArray()} method, this method acts as bridge between
* array-based and collection-based APIs. Further, this method allows
* precise control over the runtime type of the output array, and may,
* under certain circumstances, be used to save allocation costs.
*
* <p>Suppose <tt>x</tt> is a list known to contain only strings.
* The following code can be used to dump the list into a newly
* allocated array of <tt>String</tt>:
*
* <pre>{@code
* String[] y = x.toArray(new String[0]);
* }</pre>
*
* Note that <tt>toArray(new Object[0])</tt> is identical in function to
* <tt>toArray()</tt>.
*
* @param a the array into which the elements of this list are to
* be stored, if it is big enough; otherwise, a new array of the
* same runtime type is allocated for this purpose.
* @return an array containing the elements of this list
* @throws ArrayStoreException if the runtime type of the specified array
* is not a supertype of the runtime type of every element in
* this list
* @throws NullPointerException if the specified array is null
*/
<T> T[] toArray(T[] a);

从源码说明中可以看出,如果想要list.toArray()方法返回指定类型的数组,需要在toArray方法中加上一个数组类型的参数,如果参数数组个数大于list数组则将数组中多余的元素赋为空,如果数组大小不够会重新分配一个指定类型与list大小一致的数组。因此,List To Array可以写成:

1
2
3
4
5
list.toArray(new String[0]);
or
list.toArray(new String[list.size()]); //better
or
list.stream().toArray(String[]::new); //JDK 1.8
如果有收获,可以请我喝杯咖啡!