The pit in the Arrays.aslist

Source: Internet
Author: User

Objective

Recently in the project on the line to find a problem, from the background error log see:java.lang.UnsupportedOperationException Exception
From the point of view of the code, it turns out that when a arrays.aslist () method is used to convert an array to a list of lists, the Add () and remove () actions are performed on the resulting list.

For this question, now to summarize, of course, will summarize some of the pits below arrays.

Source Code Analysis

First of all, the problem is not scary, the problem is solved, but it must be ensured that the same problem will not be repeated next time.
Arrays.aslist returns the same ArrayList, why can't I use the Add and remove methods?

1, view arrays.aslist source code
,

2. View this ArrayList structure:

3, in view abstractlist structure:

Sure enough, Unsupportedoperationexception is thrown here, because the ArrayList in arrays does not implement this method, so throws an exception.
So the list returned by Arrays.aslist is a non-variable-length listing, which no longer has many features of the original list, so use the Arrays.aslist method with caution.

Other pits in arrays

1, what is the program output below?

Print result is: 1
By the above Aslist source code we can see the return of the Arrays of the inner class ArrayList constructor method receives an array of type T, and the basic type is not as a generic parameter, so here parameter a can only receive reference type, naturally in order to compile through the compiler on the above int[] Array as a reference parameter, so size is 1, to modify the problem is very simple, will int[] replaced with integer[] can. So the primitive type cannot be used as a parameter to the Arrays.aslist method, otherwise it will be treated as a parameter.

2,collections.toarray Error problem
We can look at the Java.util.ArrayList source code in the special marked with a sentence as follows:

Bug Address: https://bugs.java.com/view_bug.do?bug_id=6260652
Well, here's a test under what circumstances this exception will occur:

For example, this console prints the following results:
class [Ljava.lang.String;
Exception in thread "main" Java.lang.ArrayStoreException:java.lang.Object

We view the ToArray source code for ArrayList in arrays:

Because Aslist returns a string array, the ToArray returned here is actually the string[] type, except that it does an upward transformation to convert the string[] type to object[] type.
Note, however, that although the returned reference is object[], the actual type is string[], which is an error when you add an element to a reference type and an object that does not match the actual type.
In particular, you can refer to the relevant knowledge points for Java up-and-down transformation.

About arrays in the pit is here, there are additional welcome messages.

The pit in the Arrays.aslist

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.