How to avoid exceptions when converting Java arrays to lists

Source: Internet
Author: User
Tags arrays

Now convert a Java array to List. You can use the Arrays. toList method, but using it almost certainly produces annoying surprises.

Package com. wordpress. mlangc. arrays; import java. util. arrays; public class ArraysToList {public static void main (final String [] args) {System. out. println (Arrays. asList (new String [] {"a", "B"}); System. out. println (Arrays. asList (new Integer [] {1, 2}); System. out. println (Arrays. asList (new int [] {1, 2}); System. out. println (Arrays. asList (new String [] {"a", "B"}, "c "));}}


Because Javadoc's description of Arrays. asList is rather vague, it may be difficult for you to predict the running results of the program. Next we will reveal the answer step by step:

Line 3 outputs "[a, B]" In our console as we predicted based on the API. This is exactly what we would like to see.
In the same way as expected, row 12th outputs "[1, 2]".
The 15th rows are different. Of course, this is not to say that 15 is different from 12, but that one is int and the other is Integer, therefore, the result "[[I @ 39172e08]" is printed in our console, which is no longer as expected. We get a list that contains an address string that identifies the uniqueness of each element in an array, instead of a list that contains two Integer objects.
When we see the above results, we will not be surprised at the results output in line 18th, similar to [[Ljava. lang. String; @ 20cf2c80, c.

But what happened? The first two print statements are the same as our expected results, because the Java Language Specification specifies that calling a declaration is foo (T... T). For example, foo (new T [] {bar, baz}) is equivalent to calling foo (bar, baz. In the Arrays. asList method, T is a parameter type, so it must be of the Object type, but int is not, but int [] is. This is why the declaration of row 16th is equivalent to Arrays. asList (new Object [] {new int [] {1, 2 }}).

Arrays. asList (new Object [] {new int [] {1, 2 }})

Finally, it is very important to note that the 19th-line statement has triggered a call problem from the very beginning. We tell the compiler that we need a list containing String arrays and strings, as we expected, we get what we want.

I have explained so much so far, but we can also learn more from it: the real source of the problem is not the poor design of variable parameters. On the contrary, I think this design is good. This issue has been clearly explained in the 42nd specification of objective Java2, Arrays. asList violates this specification. In fact, Arrays. asList, as a negative textbook, tells us why we should be very careful when using Java's variable parameter design API. I won't repeat the answer in that article here, but you do need to read it yourself, however, considering the integrity, I must point out that the problematic statement will report an error when compiling with the Java1.4 compiler. This is quite good. Now we still use Arrays. asList, but for security, we need to know the complexity of the problem. The following are the rules we need to follow when converting an array to lists, so as to ensure that there is no unexpected situation:

If you want to convert an array to a list, you 'd better convert it to a string. Use Arrays. toString instead of the above method. This method does not cause any problems even for arrays of basic types.
If you want to convert an array of the basic type to a list of the corresponding encapsulation type, use Apache Commons Lang. You may have used this in the project for a long time, use ArrayUtils as follows. toObject:

List <Integer> list = Arrays. asList (ArrayUtils. toObject (new int [] {1, 2 }));

Note: In general, we recommend that you use an array of raw data types instead of a list of packed raw data types.

If you want to convert an array of the reference type to list, you can directly use Arrays. asList:

List <String> list = Arrays. asList (new String [] {"a", "B "});

Don't forget to tell people who work with you to make sure they don't make the same mistake with you. Of course, you can also choose to remember those using Arrays. problems may occur in the asList method and are replaced by common for loops. However, this will make your code messy and cause performance problems.

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.