Arrays.aslist () is the array as a list
The problem arises from:
Public class Test { publicstaticvoid main (string[] args) { int[] A = {1,2,3,4}; = Arrays.aslist (a); System.out.println (List.size ()); // 1 }}
The expected output is that the list also has 4 elements, which is the size of 4, but the result is 1.
The reasons are as follows:
In Arrays.aslist, the method accepts a variable-length parameter, which is generally considered an array parameter, but because int[] is itself a type, so when a variable is passed as a parameter, the compiler thinks that it only passes a variable whose type is an int array, so size is 1, is equivalent to the number of arrays in the list. The basic type is not a generic parameter, the wrapper type should be used, but there is no error, because the array can be generalized, so after the conversion there is an array of type int in the list
/*** Returns a fixed-size list backed by the specified array. (Changes to * The returned list "write through" to the array.) This method acts * as bridge between array-based and collection-based APIs, in * combination with {@linkCollection#toarray}. The returned list is * Serializable and implements {@linkrandomaccess}. * * <p>this method also provides a convenient-to-Create a fixed-size * list initialized to contain sever Al elements: * <pre> * list< String> Stooges = Arrays.aslist ("Larry", "Moe", "Curly"); * </pre> * *@parama The array by which the list would be backed *@returnA list view of the specified array*/@SafeVarargs Public Static<T> list<t>aslist (T ... a) {return NewArraylist<>(a); }
Returns a list of fixed sizes supported by the specified array. ( changes to the returned list are "written straight" to the array .) This approach, together with Collection.toarray , serves as a bridge between an array-based API and an Collection-based API. The returned list is serializable.
Therefore, if you are creating multiple lists, it is best to use the arrays.copyof (a) method when passing parameters, otherwise the change to the list is equivalent to the change of the array.
Public class Test { publicstaticvoid main (string[] args) { = {1,2,3,4}; = arrays.aslist (a); System.out.println (List.size ()); // 4 }}
Finally, if the integer[] array is not assigned, the default is null, instead of the default is 0, as opposed to the int[] array.
Java--arrays.aslist () method