Arrays.asList use errors

        Arrays.asList jar package using import java.util.Arrays; Arrays.asList jar using the method of ArrayList

 

1, the basic type of the array

Int [] intArr={1,2,3};

List listArr=Arrays.asList(intArr);

System.out.println (listArr.size ()); /// length 1

Summary: a variable length parameter Arrays.ArrayList generic, while the basic type (int, boolean, char, etc.) can not be of too generic, so the int [] arry array as a generic object in a collection it is only the final an element arr, it is 1 length

Rather, not a basic type String, if the code is on top of type String: stringArr = { "1", "2", "3"};

List listArr=Arrays.asList(stringArr);

System.out.println (listArr.size ()); /// length 3

 

2, fixed length

The reason is because it is a fixed length does not override add, remove method, once an element is initialized so that the set size is immutable

        String [] arr = { "today", "a", "sunny day"};

        List list=Arrays.asList(arr);

        List.add ( "new");

        List.remove ( "Watch");

        Thrown Information: java.lang.UnsupportedOperationException (unsupported operating instructions)

 

 

 

3, Arrays.ArrayList Quest

 

 

public class Arrays {
    @SafeVarargs
    @SuppressWarnings("varargs")
    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) {
            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);
        }
}
}
View Code

 

Arrays.ArrayList tools Arrays is an internal static class, it does not fully implement the method List, and ArrayList directly implements List interface, implements List all the methods.

Arrays.ArrayList external reference to an array directly through the "=" gives a generic internal array, so the essence of a point to the same array. 

4, into an array list by:

A): If the frame is developed in the spring:

import org.springframework.util.CollectionUtils;

CollectionUtils.arrayToList(arr);

 

b): traverse

for(String str:arryList){

          list.add(str);

}

c): Collections Tools

Collections.addAll(list, "a","b","c");

 

 

Guess you like

Origin www.cnblogs.com/enhance/p/11004196.html