Java--arrays.aslist () method

Source: Internet
Author: User

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&lt; String&gt;     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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.