Discussion on the use of Arrays. asList of method

  • 2020-06-01 09:54:03
  • OfStack

First, the method converts the array to list. The following points need to be noted:

(1) this method does not apply to 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 one is automatically updated

(3) add and remove methods are not supported

The code:


package com.hdu.test;
import java.util.Arrays;
import java.util.List;
abstract public class AsllistTest {
 public static void main(String[] args) {
  String[] s = {"aa","bb","cc"};
  List<String> strlist = Arrays.asList(s);
  for(String str:strlist){
   System.out.println(str);
  }
  System.out.println("------------------------");
  // The base data type results are printed as 1 An element 
  int[] i ={11,22,33}; 
  List intlist = Arrays.asList(i);
  for(Object o:intlist){
   System.out.println(o.toString());
  }
  System.out.println("------------------------");
  Integer[] ob = {11,22,33};
  List<Integer> oblist = Arrays.asList(ob);
  for(int a:oblist){
   System.out.println(a);
  }
  System.out.println("------------------------");
 }
}

Operation results:


aa
bb
cc
------------------------
[I@15db9742
------------------------
22
------------------------

Please refer to this article: https: / / www ofstack. com article / 104399. htm


Related articles: