Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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;
    }
}
like image 916
epoch Avatar asked Nov 05 '13 14:11

epoch


People also ask

What is Java Util arrays ArrayList?

The java. util. Arrays$ArrayList is a nested class inside the Arrays class. It is a fixed size or immutable list backed by an array.

Does Java util * Include ArrayList?

ArrayList is a part of collection framework and is present in java. util package.

Why do we use ArrayList instead of array in Java?

In short, ArrayList is more flexible than a plain native array because it's dynamic. It can grow itself when needed, which is not possible with the native array. ArrayList also allows you to remove elements which are not possible with native arrays.

Can you have an ArrayList of arrays in Java?

ArrayList of arrays can be created just like any other objects using ArrayList constructor. In 2D arrays, it might happen that most of the part in the array is empty. For optimizing the space complexity, Arraylist of arrays can be used.


Video Answer


2 Answers

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.

like image 84
Russell Zahniser Avatar answered Oct 17 '22 17:10

Russell Zahniser


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

like image 4
arynaq Avatar answered Oct 17 '22 17:10

arynaq