Java中的数组与List相互转换的方法分析

目录

一、Java中的数组转换为List的方法

1、使用Arrays.asList()方法

2、使用Collections.addAll()方法

3、使用集合的addAll()方法

4、使用Spring框架将数组转换为List

二、Java中将List类型转换为数组的方法

1、使用List.toArray()方法

三、几点问题的释疑分析:


一、Java中的数组转换为List的方法

1、使用Arrays.asList()方法

String[] strArray = {"中国", "美国", "日本", "韩国","英国", "法国", "德国"};

List<String> list = Arrays.asList(strArray);

对于Arrays.asList()方法需要注意以下几点:

1)该方法返回的是基于数组的List视图(List view,类似于c++中的指针变量或对象引用)。所以,这种方式是将数组转换为List的最快的方式。因为返回的只是视图,不需要多余的内存来创建新的List以及复制操作。

2)该方法返回的List是长度是固定的(fixed),只是元素个数是固定的,并不是元素的内容是只读的,元素的内容是可以修改的,所以我们不能进行删除、添加操作,但却可以使用set()方法进行修改元素操作。如果你对返回的List执行add()添加新元素,会返回UnsupportedOperationException。至于为什么报这个异常,文章末尾给出了分析。

3)因为该方法返回的是基于原数组的List视图,所以,当我们使用set方法修改了List中的元素的时候,那么原来的数组也会跟着改变(这是视图的特性)。

4)从java 5开始,该方法支持泛型,所以我们可以从数组中得到类型安全ArrayList。

特别注意:

1)如果我们想让转换为只读的List,可以使用Collections.unmodifiableList()方法来将数组转换为指定List。

2)如果想返回的方法能够进行添加、删除元素操作,则可以使用new ArrayList(Arrays.asList(array)) ,这样就会创建一个对象类型的ArrayList,并将数组的内容复制一份过去。

2、使用Collections.addAll()方法

该方法没有第一种方法高效,但是更加灵活。同样也是新建一个ArrayList,将数组的内容复制进去。

String[] strArray = {"中国", "美国", "日本", "韩国","英国", "法国", "德国"};
List<String> list = new ArrayList<String>();
Collections.addAll(list, strArray);

对于该方法需要了解的:

1)没有Arrays.asList()快,但是更加灵活。

2)该方法实际上是将数组的内容复制到ArrayList中。

3)因为是复制内容到ArrayList中,所以我们对ArrayList进行修改、添加、删除操作都不会影响原来的数组。

4)该方法相当于一个添加操作。该方法并不会覆盖ArrayList中已经存在的元素。如下:

String[] fooArray = {"one", "two", "three"};
List<String> list = new ArrayList();
Collections.addAll(list,fooArray);
Collections.addAll(list,fooArray);
System.out.println(list);

输出为:

[one, two, three, one, two, three]

3、使用集合的addAll()方法

List list = new Arraylist();
list.addAll(Arrays.asList(list));

4、使用Spring框架将数组转换为List

Spring框架中的CollectionUtils提供了几个方法来将数组转换为Arraylist。例如:

CollectionUtils.arrayToList();

当然,返回的List是不可修改的,不能add()或remove()元素。

String [] currency = {"SGD", "USD", "INR", "GBP", "AUD", "SGD"};
System.out.println("Size of array: " + currency.length);
List<String> currencyList = CollectionUtils.arrayToList(currency);
      
//currencyList.add("JPY"); //Exception in thread "main" java.lang.UnsupportedOperationException
//currencyList.remove("GBP");//Exception in thread "main" java.lang.UnsupportedOperationException
 
System.out.println("Size of List: " + currencyList.size());
System.out.println(currencyList);

二、Java中将List类型转换为数组的方法

1、使用List.toArray()方法:

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("中国");
        list.add("美国");
        list.add("日本");
        list.add("韩国");
        list.add("俄罗斯");
        list.add("白罗斯");

        String[] strArray = new String[list.size()];

        list.toArray(strArray);
    }
}

三、几点问题的释疑分析:

A.为何对Arrays.asList()返回的List进行添加、删除操作会报错,而set方法却可以使用?

我们来看下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}.
     *
     * <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 a the array by which the list will be backed
     * @return a list view of the specified array
     */
    @SafeVarargs
    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }
 
    /**
     * @serial include
     */
    private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
        private final E[] a;
 
        ArrayList(E[] array) {
            if (array==null)
                throw new NullPointerException();
            a = array;
        }
 
        public int size() {
            return a.length;
        }
 
        public Object[] toArray() {
            return a.clone();
        }
 
        public <T> T[] toArray(T[] a) {
            int size = size();
            if (a.length < size)
                return Arrays.copyOf(this.a, size,
                                     (Class<? extends T[]>) a.getClass());
            System.arraycopy(this.a, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }
 
        public E get(int index) {
            return a[index];
        }
 
        public E set(int index, E element) {
            E oldValue = a[index];
            a[index] = element;
            return oldValue;
        }
 
        public int indexOf(Object o) {
            if (o==null) {
                for (int i=0; i<a.length; i++)
                    if (a[i]==null)
                        return i;
            } else {
                for (int i=0; i<a.length; i++)
                    if (o.equals(a[i]))
                        return i;
            }
            return -1;
        }
 
        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }
    }
...

分析:

我们注意到Arrays.asList()方法返回的是个内部的ArrayList,这个类同样是AbstractList的一种实现,AbstractList的add和remove方法会有异常抛出:

  /**
     * {@inheritDoc}
     *
     * <p>This implementation always throws an
     * {@code UnsupportedOperationException}.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     * @throws IllegalArgumentException      {@inheritDoc}
     * @throws IndexOutOfBoundsException     {@inheritDoc}
     */
    public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }
 
    /**
     * {@inheritDoc}
     *
     * <p>This implementation always throws an
     * {@code UnsupportedOperationException}.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws IndexOutOfBoundsException     {@inheritDoc}
     */
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }

而该ArrayList就只有以下方法,并没有实现add和remove方法:

contain(Object)
get(int)
indexOf(Object)
set(int)
size()
toArray()
toArray(T[])

根本就没有add()和remove()方法,所以 当我们对返回的List执行add和remove方法时,就会报UnsupportedOperationException了。但是因为有set()方法,所以,我们可以修改返回的List。

B.我们说Arrays.asList()返回的是基于原数组的List视图, 而且修改List的元素时候,原数组的内容也会同时改变,这又是为何呢?

分析:

根据上面的代码,我们可以知道, 我们调用返回的ArrayList的set(),get(), indexOf(), contain(),size()这些方法,本质上都是去对原数组进行对应的操作。所以,我们改变返回的ArrayList中的内容的时候,原数组也会同时改变。这就是集合视图(collection view),集合了常用的方法。

C.为何返回的ArrayList的长度是固定的?还有为什么Arrays.asList()方法最快?

分析:

还是上面的代码,一般来说,ArrayList内部有一个对象类型数组作为实例变量来存放ArrayList中的数据。而上面的内部类中,ArrayList的这个实例变量就是a,而它只是将引用指向了原数组,并未将原数组的内容复制到a中。这样就没有进行复制操作,也没有创建新的数组对象,自然最快了。

同时,该内部类ArrayList并为提供add方法等方法,自然是无法修改ArrayList的长度。而且因为是直接将实例变量a指向原数组,我们知道数组一旦初始化后就没法修改它的大小了,所以原数组不能改变大小,自然返回的ArrayList的长度也不能改变长度,长度就只能是固定的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值