Problem reproduction

Arrays.aslist () is often used in daily development to easily initialize a List, but there is a very strange problem with the test code as follows:

    @Test
    public void test(a) {
        List<Integer> ids=Arrays.asList(1.2);
        ids.add(3);
    }
Copy the code

The results

java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)
	at com.zjrb.media.media.MediaTest.test(MediaTest.java:36)
Copy the code

Isn’t this code too common? Initializing a List and then adding an element to the List would result in an unsupported error. Arrays.adlist () still has some potholes, so here’s a summary of them.

1. Add (), remove(), add(), removeAll()… The add/remove method is not available

    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }


    private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess.java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
        private finalE[] a; ArrayList(E[] array) { a = Objects.requireNonNull(array); }}Copy the code

See Arrays. AsList () source code can be found that method returns the ArrayList isn’t Java. Utill the ArrayList but Java. Utill. Arrays. The ArrayList, Private final E[] a; This is an immutable array, so add(), remove(), addAll(), removeAll() are not available. Of course, the set() method is still available to modify the value of an array variable.

2. Primitive arrays are not supported

The test code is as follows

        int [] a={1.2};
        Integer [] b={1.2};
        System.out.println(Arrays.asList(a));
        System.out.println(Arrays.asList(b));
Copy the code

The output is:

[[I@cb29b75]
[1.2]
Copy the code

Public static

List

asList(T… A) The argument received is a generic T. The primitive type does not support generics, so you can only return a single value in the List as a reference to the array int [], and certainly not the value you want.

3. The toArrays() method returns different data types

Java.util. ArrayList test code:

        List<Integer> a=new ArrayList<>();
        a.add(1);
        Object [] objects=a.toArray();
        System.out.println(objects.getClass());
        Ljava.lang.Object;
Copy the code

Arrays.aslist ()

        List<Integer> a=Arrays.asList(1);
        Object [] objects=a.toArray();
        System.out.println(objects.getClass());
        // Output class [ljava.lang. Integer;
Copy the code

Array.aslist toArray()

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

These return the clone() of T[]a, which returns the same type as Integer [], but with Object[] cast up.

Java.util.arraylist toArray()

       public Object[] toArray() {
        return Arrays.copyOf(elementData, size);
    }
    
Copy the code

Java.utill.arraylist toArray() returns the Object[] type. So be careful when you actually use it.

4. Modify the original parameter pit

Java.util. ArrayList tests the code

        Integer [] a={1.2.3};
        List<Integer> list=new ArrayList<>();
        Collections.addAll(list,a);
        a[0] =4;
        System.out.println(list);
        // output [1,2,3]
Copy the code

Arrays.asList

        Integer [] a={1.2.3};
        List<Integer> list=Arrays.asList(a);
        a[0] =4;
        System.out.println(list);
        // Output [4, 2, 3]
Copy the code

Arrays.aslist () calls a = objects.requirenonNULL (array); , is just a reference, so changing the value of the original parameter changes the value of the array

conclusion

Arrays.aslist () found these potholes so far, so please be careful when using them, and don’t tread on any potholes that have been trodden on before. If you find other pits welcome to share!