Array.aslist: You must know the "trap" when you go to List of arrays!

Source: Internet
Author: User

Recently developed, business processing, often used to aslist method, which let me not think of its many easy to make people wrong place or misunderstanding, so I want to spare time to tidy up, and everyone to share out, late at night, words do not say, mainly in code-based, simple code, you can see!

Everyone knows that this method is to convert an array into a list, which is a static method of the arrays class in the Java.util package in the JDK. Be sure to pay attention when you use it (see Code and comments, as you see):

[Java]View Plaincopy
  1. String s[]={"AA","BB","CC"};
  2. List<string> Slist=arrays.aslist (s);
  3. for (String str:slist) {//can traverse out individual elements
  4. System.out.println (str);
  5. }
  6. System.out.println (Slist.size ()); //Is 3
  7. System.out.println ("-----------");
  8. int i[]={,33};
  9. List intlist=arrays.aslist (i); There is an integer array type object in//intlist, and the entire array is stored as an element in the
  10. for (Object o:intlist) {//an element
  11. System.out.println (O.tostring ());
  12. }
  13. System.out.println ("-----------");
  14. Integer ob[]={,33};
  15. List<integer> objlist=arrays.aslist (OB); each element in the array is a single element in the list
  16. for (int a:objlist) {
  17. System.out.println (a);
  18. }
  19. System.out.println ("-----------");
  20. Objlist.remove (0);//aslist () returns the ultimate ArrayList type in arrays, which has a set,get,contains method, but does not add and remove elements, so the size is fixed, will be error
  21. Objlist.add (0);//Because there is no Add method in the implementation class of the list returned by Aslist, the error will be

Input Result:

[Java]View Plaincopy
    1. Aa
    2. Bb
    3. Cc
    4. 3
    5. - - - - - - - - - - -
    6. [I@287efdd8
    7. - - - - - - - - - - -
    8. 11
    9. 22
    10. 33
    11. - - - - - - - - - - -

The reason why, see Aslist Source code to understand:

[Java]View Plaincopy
    1. Public static <T> list<t> aslist (T ... a) {
    2. return New Arraylist<t> (a);
    3. }
[Java]View Plaincopy
[Java]View Plaincopy
[Java]View Plaincopy
    1. Private final e[] A;
    2. ArrayList (e[] array) {
    3. if (array==null)
    4. throw new NullPointerException ();
    5. A = array;
    6. }



If you want to get a new normal list based on the array, of course you can loop one add, or you can have the following 2 ways:

[Java]View Plaincopy
  1. arraylist<integer> copyarrays=New arraylist<integer> (Arrays.aslist (OB)); This is to get a new list, which can be add,remove.
  2. Copyarrays.add (222); Normal, no error.
  3. Collections.addall (new arraylist<integer> (5), OB); Or create a new empty list and add the array you want to convert with Collections.addall


If you want to go directly to list with aslist based on an array of basic types, such as int[],long[], then we can choose to use the Toobject () method of the Array tool class Arrayutils class in Apache Commons-lang Toolkit, which is very convenient As follows:

[Java]View Plaincopy
    1. Arrays.aslist (Arrayutils.toobject (i)); ///Upper code: int i[]={11,22,33};, to achieve the effect we want


This class features a powerful feature:

can also "reverse" turn around, specifically do not say.

Array.aslist: You must know the "trap" when you go to List of arrays!

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.