Java study notes-Arrays.asList

First, the method is to convert the array to a list . The following points need to be noted:

  (1) This method is not suitable for basic data types (byte, short, int, long, float, double, boolean)

  (2) This method links the array with the list, and when one of them is updated, the other is automatically updated

  (3) The add and remove methods are not supported

In other words, Arrays.asList can only be used for traversal, and cannot be used for other methods of adding and deleting

 

Test code

public class Test {
    public static void main(String[] args) {
       String s="小周,小艾,小晓";
       System.out.println(Arrays.asList(s.split(",")));

       Integer i=123990;
       System.out.println(Arrays.asList(i));

    }

}

 

What if you want to have add and remove methods for Arrays.asList? Then do the following:

List<String> arrayList=new ArrayList<>(list);
arrayList.add("4");

public class Test {
    public static void main(String[] args) {
      String s="1,2,3";
      List<String> list=Arrays.asList(s.split(","));

      System.out.println(list);
      List<String> arrayList=new ArrayList<>(list);
      arrayList.add("4");
      System.out.println(arrayList);
    }
}

 

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/112196654