为什么会出现java.util.Arrays中在一个ArrayList声明 [英] Why is there an ArrayList declaration within java.util.Arrays

查看:85
本文介绍了为什么会出现java.util.Arrays中在一个ArrayList声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JDK 1.7中,有哪些是用于 asList 的ArrayList 声明。

他们为什么作出新的私有静态类而不是使用的java.util.ArrayList

  @SafeVarargs
公共静态< T>清单< T> asList(T ...一){
    返回新的ArrayList<>(一);
}/ **
 * @serial包括
 * /
私有静态类的ArrayList< E>扩展AbstractList的< E>
    实现了RandomAccess,java.io.Serializable接口
{
    私有静态最后的serialVersionUID长= -2764017481108945198L;
    私人最终E []一;    ArrayList的(E []数组){
        如果(阵列== NULL)
            抛出新的NullPointerException();
        A =阵列;
    }    公众诠释大小(){
        返回则为a.length;
    }    公共Object []对象的toArray(){
        返回a.clone();
    }    公众< T> T []的toArray(T [] A){
        INT大小=();
        如果(则为a.length<大小)
            返回Arrays.copyOf(this.a,大小
                                 (类&LT ;?扩展T []>)a.getClass());
        System.arraycopy(this.a,0,A,0,大小);
        如果(则为a.length>大小)
            一个[大小] = NULL;
        返回;
    }    公共电子GET(INT指数){
        返回[指数]
    }    公共电子集(INT指数,E元素){
        Ë属性oldValue = A [指数]
        一个[索引] =元素;
        返回的属性oldValue;
    }    公众诠释的indexOf(对象o){
        如果(O == NULL){
            的for(int i = 0; I<则为a.length;我++)
                如果(一个由[i] == NULL)
                    返回我;
        }其他{
            的for(int i = 0; I<则为a.length;我++)
                如果(o.equals(一个由[i]))
                    返回我;
        }
        返回-1;
    }    公共布尔包含(对象o){
        返回的indexOf(O)!= -1;
    }
}


解决方案

由于由返回列表 <$c$c>Arrays.asList()是的的给定的数组支持。它包装数组;该阵列的变化会反映在列表,反之亦然。

另外,作为其结果,在列表返回这里有一个固定的大小。因此,它不能是一个的ArrayList ,因为的ArrayList 可以扩大或缩小。

In JDK 1.7, there is an ArrayList declaration which is used by asList.

Why did they make a new private static class and not use java.util.ArrayList:

@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;
    }
}

解决方案

Because the List returned by Arrays.asList() is backed by the given array. It wraps that array; changes to the array are reflected in the List and vice versa.

Also, as a result of this, the List returned here has a fixed size. So, it can't be an ArrayList because ArrayList can grow or shrink.

这篇关于为什么会出现java.util.Arrays中在一个ArrayList声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆