Problems encountered when using Arrays.asList ()

     Arrays.asList () carrying a JDK is a method of converting a set of the array, but found in the use, with this embodiment, after the array into a collection, the corresponding set of modified using methods throw exceptions,

If the add / remove / clear JDK and other open source code, find the method that returns an ArrayList, but we enter into the internal ArrayList, we found that the ArrayList, and we normally use is not the same, the source code is as follows:

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) {
            a = Objects.requireNonNull(array);
        }

        @Override
        public int size() {
            return a.length;
        }

        @Override
        public Object[] toArray() {
            return a.clone();
        }

        @Override
        @SuppressWarnings("unchecked")
        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;
        }

        @Override
        public E get(int index) {
            return a[index];
        }

        @Override
        public E set(int index, E element) {
            E oldValue = a[index];
            a[index] = element;
            return oldValue;
        }

        @Override
        public int indexOf(Object o) {
            E[] a = this.a;
            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;
        }

        @Override
        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }

        @Override
        public Spliterator<E> spliterator() {
            return Spliterators.spliterator(a, Spliterator.ORDERED);
        }

        @Override
        public void forEach(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            for (E e : a) {
                action.accept(e);
            }
        }

        @Override
        public void replaceAll(UnaryOperator<E> operator) {
            Objects.requireNonNull(operator);
            E[] a = this.a;
            for (int i = 0; i < a.length; i++) {
                a[i] = operator.apply(a[i]);
            }
        }

        @Override
        public void sort(Comparator<? super E> c) {
            Arrays.sort(a, c);
        }
    }

 This is just an internal ArrayList class Arrays also inherited AbstractList abstract class, that is to say, Arrays.asList () method returns an instance of java.util.Array.ArrayList instead java.util.ArrayList instance,

They then so what difference does it make? Spoke of before, the class uses a set of modified method throws UnsupportedOperationException exception, so look at the modification method involved, we found the class inherits the abstract class AbstractList,

But does not implement a corresponding modification method, therefore thrown exception AbstractList source to an abstract class, Arrays.asList () method returns a collection surface, or a real array, the development is to be noted, in particular

Use the premise of this approach is certainly not using a set of corresponding modification method, Ali has also developed standardized labeling this one since it is the array to a collection certainly there are other alternatives:

(1) List list = new ArrayList<>(Arrays.asList("a", "b", "c"))再包裹一层;

(2) the use of native methods, through the array, placed in a new in new java.util.ArrayList;

(3) List<String> resultList = new ArrayList<>(array.length);

  Collections.addAll(resultList,array);

     Use Collections.addAllway;

(4)List<String> resultList = List.of(array);该方式仅限java9

Understand Arrays.asList method will find that the design is a very sad way, but there is reasonable, and the methods are the same as for java.util.ArrayList enhanced array, ArrayList are two different people design, JDK also remains

The design, as long as we pay attention to in the development not jump pit on the line

Guess you like

Origin www.cnblogs.com/zhexuejun/p/11525546.html