This paper is translated by importnew-fluttering leaf from MLANGC. Welcome to join the translation team. Reproduced please see at the end of the request.
Unfortunately, not everything is as satisfactory as it is. For example, now convert a Java array to a list. Of course, we can use the Arrays.aslist method, but if you don't think about it, you'll almost certainly have unpleasant surprises. Consider the following procedure and predict its output you'll see what I mean:
123456789101112131415161718192021 |
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"
));
}
}
|
Since Javadoc 's description of Arrays.aslist is quite vague, it may be a bit difficult for you to predict the outcome of the program, let's take a step-by-step answer:
- Line 9th, as we have predicted by the API, outputs "[A, b]" in our console, which is what we'd like to see.
- Line 12th also outputs "[up]" as expected.
- Line 15th is different, of course, this is not to say that the difference between 15 and 12, but one is an int is the other is an integer, so in our console printed a similar result "[[[[Email]]", which is no longer as expected. Protected Instead of a list containing two integer objects, we get a list of address strings that contain the uniqueness of each element in the array.
- When you see the above results, you will not be surprised at the result of a similar "[[Ljava.lang.string;@20cf2c80, C]" output for line 18th.
But what happened? The first two print statements are the same as we expected because the Java language specification calls for a method called Foo (t ... t), such as foo (new T[]{bar,baz}) equivalent to a Call of Foo (Bar,baz). In the Arrays.aslist method, T is a parameter type, so it must be an object type, but int is not, and int[] is. This is why the declaration of line 16th is equivalent to arrays.aslist (new object[] {new int[] {1, 2}}).
1 |
Arrays.asList( new Object[] { new int [] { 1 , 2 } }) |
Finally, it is also very important that the declaration on line 19th produces a calling problem from the beginning. We tell the compiler that we need a list that contains string arrays and strings, as we expected, and we get what we want.
So much has been explained so far, but we can learn more from it: the real source of the problem is not the bad design of the mutable parameters; On the contrary, I think the design is very good. This question has been explained clearly in the 42nd norm of the effective Java2, Arrays.aslist violated this specification, and in fact arrays.aslist as a negative case, telling us why we should be very careful when using the variable parameter design API for Java. I'm not going to repeat the answer in that article, but you really need to read it yourself, but given the completeness I must point out that the statement above has an error when compiling with the Java1.4 compiler, which is pretty good. Now we still use arrays.aslist, but for security we know the complexities of the problems we face. Here are the rules we need to follow when converting an array to lists to ensure that nothing unexpected happens:
- If you want to convert an array to a list just to convert it to a string, it is best to use arrays.tostring instead of the above method. There is no problem with this method even for arrays of primitive types.
- If you are going to convert a primitive type array to a list of the corresponding package type, use Apache Commons Lang Bar, perhaps your project is using it, similar to the following using Arrayutils.toobject:
1 |
List<Integer> list = Arrays.asList(ArrayUtils.toObject( new int [] { 1 , 2 })); |
Please note:It is generally recommended to use an array of raw data types instead of the boxed raw data type list.
- If you intend to convert an array of reference types to list, you can use Arrays.aslist directly:
1 |
List<String> list = Arrays.asList( new String[] { "a" , "b" }); |
Don't forget to tell the people you work with to make sure they don't make the same mistake as you. Of course, you can also choose to remember only those places where you might have problems using the arrays.aslist method and use a normal for loop instead, but that would clutter your code and lead to performance problems .
Original link: MLANGC translation: importnew.com-fluttering leaf
Link: http://www.importnew.com/14996.html
Considerations when converting a Java array to a list