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进行修改时会出现下面的异常:

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)

测试代码为:

public class Test {
   public static void main(String[] args) {
       String[] str = {"134", "test", "list"};
       List list = Arrays.asList(str);
       list.add("");
    }
}

为什么会出现这个错误呢?看一下Arrays.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 {@link Collection#toArray}.  The returned list is
     * serializable and implements {@link 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");
     * 
* * @param 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 List asList(T... a) { return new ArrayList<>(a); }

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

List list = new ArrayList(Arrays.asList(str));

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

List list = Lists.newArrayList(str);

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

List list = new ArrayList();
CollectionUtils.addAll(list, str);

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

2.List To Array

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

    /**
     * Returns an array containing all of the elements in this list in proper
     * sequence (from first to last element).
     *
     * 

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. * *

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. * *

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 null. * (This is useful in determining the length of the list only if * the caller knows that the list does not contain any null elements.) * *

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. * *

Suppose x is a list known to contain only strings. * The following code can be used to dump the list into a newly * allocated array of String: * *

{@code
     *     String[] y = x.toArray(new String[0]);
     * }
* * Note that toArray(new Object[0]) is identical in function to * toArray(). * * @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[] toArray(T[] a);

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

list.toArray(new String[0]);
or
list.toArray(new String[list.size()]); //better
or 
list.stream().toArray(String[]::new); //JDK 1.8

你可能感兴趣的:(Java中List与Array的转换)