Arrays is a tool method. Its asList (T..a) method can convert an array into a set List, but there are two points to note when using this method:
(1) Number of elements
public static void main(String[] args) { int[] datas = new int[]{1,2,3,4,5}; List list = Arrays.asList(datas); System.out.println(list.size()); }
------------Output:
1
Why 1 instead of 5? First look at the source code of asList():
public static <T> List<T> asList(T... a) { return new ArrayList<T>(a); }
Note this parameter: t... A, This parameter is a generic variable length parameter (multiple parameters can be passed in). We know that basic data types cannot be generalized, that is, eight basic data types cannot be used as generic parameters, but why does the compiler not report an error? This is because in java, arrays are treated as an object, which can be generic, so our program takes an int array as a T Type, so after conversion, there will only be one element of type int array in the List. So our program system out. println(datas.equals(List.get(0))); The output must be true. If you change int to Integer, the length becomes 5.
(2) Let's look at the following procedure:
enum Week{Sum,Mon,Tue,Web,Thu,Fri,Sat} public static void main(String[] args) { Week[] weeks = {Week.Sum,Week.Mon,Week.Tue,Week.Web,Week.Thu,Week.Fri}; List<Week> list = Arrays.asList(weeks);//The internal class ArrayList of the Arrays tool class is returned, not Java util. ArrayList, which is a collection of immutable length list.add(Week.Sat);//The inner class ArrayList itself does not provide add (), and its parent class provides add () but does not implement it, so an exception will be reported }
This program is very simple. It is about converting an array into a list, and then changing the value of the set, but how to run it?
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:131)
at java.util.AbstractList.add(AbstractList.java:91)
at com.array.Test.main(Test.java:18)
Compilation is correct, but there is an abnormal error in running! Unsupported operationexception, which is thrown when the requested operation is not supported. To some extent, the add method is not supported. We know it is impossible! What causes this anomaly? First look at the source code of asList():
public static <T> List<T> asList(T... a) { return new ArrayList<T>(a); }
Here, an ArrayList object is returned directly, but note that this ArrayList is not Java util. ArrayList, but an internal class of the Arrays tool class:
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; } /** Omitting method**/ }
If this inner class does not provide an add() method, check the parent class AbstractList:
public boolean add(E e) { add(size(), e); return true; } public void add(int index, E element) { throw new UnsupportedOperationException(); }
Here, the parent class only provides methods, but the specific implementation of the methods does not. It is hoped that the child classes will implement the methods. However, the internal class ArrayList does not provide the implementation method of add. In the internal class ArrayList of Arrays, it mainly provides the following methods:
1. size: number of elements
2. toArray: it is converted into an array to realize the shallow copy of the array.
3. Get: get the specified element.
4. contains: whether an element is included.
So to sum up, asList(T..a) returns a list with an immutable length. We cannot increase or decrease the length of the array and the converted list by add ing or removing.