Why is there an ArrayList declaration within java.util.Arrays

半世苍凉 提交于 2019-12-18 05:28:10

问题


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

回答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.




回答2:


Because the default ArrayList has no ArrayList(E[] array) constructor.




回答3:


Because they're providing a List wrapper around the provided array which is O(1), rather than building an ArrayList from the elements in the array, which is O(n).

Is this causing you a problem? If so, it's probably because you're declaring your variables as ArrayList when you should be using List




回答4:


The asList(T... a) methods is declared to return a fixed-size list backed by the specified array which should also be serializable and implements RandomAccess

java.util.ArrayList does not qualify for this definition (fixed size), so another type of java.util.List should be used.




回答5:


if you use ArrayList, fast retrieve data from Table because it's index based, where we want fast retriving data we can use Arraylist ....

But ArrayList is not faster in the time of deleting record because rearranging the index is more complex

Where we want more deleting record we can use LinkedList it's not index based

ArrayList Declaration

java.util.List

ArrayList arrayList = new ArrayList();



来源:https://stackoverflow.com/questions/19790832/why-is-there-an-arraylist-declaration-within-java-util-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!