Java array to list, so it’s like this — there’s a trap!

In recent development, the aslist method is often used for business processing, which reminds me of many places that are easy to make mistakes or misunderstandings. Therefore, I want to take time to sort out and share with you. It's late at night, and I don't say much. It's mainly code, simple code. You can see it at a glance!

As we all know, this method is to convert an array into a list, which is Java in JDK Static method of arrays class in util package. You must pay attention when using it (please see the code and comments, which will be clear at a glance):

 String s[]={"aa","bb","cc"};
        List<String> sList=Arrays.asList(s);
        for(String str:sList){//能遍历出各个元素
            System.out.println(str);
        }
        System.out.println(sList.size());//为3
        
        System.out.println("- - - - - - - - - - -");
        
        int i[]={11,22,33};
        List intList=Arrays.asList(i);    //intList中就有一个Integer数组类型的对象,整个数组作为一个元素存进去的
        for(Object o:intList){//就一个元素
            System.out.println(o.toString());
        }
        
        System.out.println("- - - - - - - - - - -");
        
        Integer ob[]={11,33};
        List<Integer> objList=Arrays.asList(ob);    //数组里的每一个元素都是作为list中的一个元素
        for(int a:objList){
            System.out.println(a);
        }
        
        System.out.println("- - - - - - - - - - -");

//objList. remove(0);// Aslist () returns the private ultimate ArrayList type in arrays. It has set, get and contains methods, but there is no method to add or delete elements. Therefore, the size is fixed and an error / / objlist. Will be reported add(0);// Since there is no add method in the implementation class of the list returned by aslist, an error will be reported. Input result:

aa
bb
cc
3
- - - - - - - - - - -
[I@287efdd8
- - - - - - - - - - -
11
22
33
- - - - - - - - - - -

For the above reasons, you can see from the source code of aslist:

public static <T> List<T> asList(T... a) {
    return new ArrayList<T>(a);
}
private final E[] a;

ArrayList(E[] array) {
    if (array==null)
        throw new NullPointerException();
    a = array;
}

If you want to get a new normal list from the array, of course, you can add it one by one, or you can have the following two methods:

 //这样就是得到一个新的list,可对其进行add,remove了
 ArrayList<Integer> copyArrays=new ArrayList<Integer>(Arrays.asList(ob));
 copyArrays.add(222);//正常,不会报错

 //或者新建一个空的list,把要转换的数组用Collections.addAll添加进去
 Collections.addAll(new ArrayList<Integer>(5),ob);

If you want to directly convert an array of basic types, such as int [], long [], into a list using aslist, you can choose to use the toobject() method of the arrayutils class of the array tool class in the Apache commons Lang toolkit, which is very convenient, as follows:

Arrays.asList(ArrayUtils.toObject(i));//上边的代码:int i[]={11,33};,达到了我们想要的效果

This class is powerful:

It can also be "reversed", not to mention the details.

---------------------Author: Java my life source: CSDN original text: https://blog.csdn.net/chenleixing/article/details/43775127

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>