java 數組轉list asList用法


https://www.cnblogs.com/zheyangsan/p/6910476.html

 

java中數組轉list使用Arrays.asList(T... a)方法。

示例:

public class App {
  public static void main(String[] args) {
    List<String> stringA = Arrays.asList("hello", "world","A");
    String[] stringArray = {"hello","world","B"};
    List<String> stringB = Arrays.asList(stringArray);
 
    System.out.println(stringA);
    System.out.println(stringB);
  }
}
 運行結果:
1
2
[hello, world, A]
[hello, world, B]
 這個方法使用起來非常方便,簡單易懂。但是需要注意以下兩點。

一、不能把基本數據類型轉化為列表

仔細觀察可以發現asList接受的參數是一個泛型的變長參數,而基本數據類型是無法泛型化的,如下所示:
public class App {
  public static void main(String[] args) {
    int[] intarray = {1, 2, 3, 4, 5};
    //List<Integer> list = Arrays.asList(intarray); 編譯通不過
    List<int[]> list = Arrays.asList(intarray);
    System.out.println(list);
  }
}
 
output:
[[I@66d3c617]
這是因為把int類型的數組當參數了,所以轉換后的列表就只包含一個int[]元素。

解決方案:

  要想把基本數據類型的數組轉化為其包裝類型的list,可以使用guava類庫的工具方法,示例如下:
int[] intArray = {1, 2, 3, 4};
List<Integer> list = Ints.asList(intArray);
 

 二、asList方法返回的是數組的一個視圖

視圖意味着,對這個list的操作都會反映在原數組上,而且這個list是定長的,不支持add、remove等改變長度的方法。
public class App {
  public static void main(String[] args) {
    int[] intArray = {1, 2, 3, 4};
    List<Integer> list = Ints.asList(intArray);
    list.set(0, 100);
    System.out.println(Arrays.toString(intArray));
    list.add(5);
    list.remove(0);
 
  }
}
 output:
[100, 2, 3, 4]
UnsupportedOperationException
UnsupportedOperationException

  

java中數組轉list使用Arrays.asList(T... a)方法。

示例:

1
2
3
4
5
6
7
8
9
10
public  class  App {
   public  static  void  main(String[] args) {
     List<String> stringA = Arrays.asList( "hello" "world" , "A" );
     String[] stringArray = { "hello" , "world" , "B" };
     List<String> stringB = Arrays.asList(stringArray);
 
     System.out.println(stringA);
     System.out.println(stringB);
   }
}

 運行結果:

1
2
[hello, world, A]
[hello, world, B]

 這個方法使用起來非常方便,簡單易懂。但是需要注意以下兩點。

一、不能把基本數據類型轉化為列表

仔細觀察可以發現asList接受的參數是一個泛型的變長參數,而基本數據類型是無法泛型化的,如下所示:

1
2
3
4
5
6
7
8
9
10
11
public  class  App {
   public  static  void  main(String[] args) {
     int [] intarray = { 1 2 3 4 5 };
     //List<Integer> list = Arrays.asList(intarray); 編譯通不過
     List< int []> list = Arrays.asList(intarray);
     System.out.println(list);
   }
}
 
output:
[[I @66d3c617 ]

這是因為把int類型的數組當參數了,所以轉換后的列表就只包含一個int[]元素。

解決方案:

  要想把基本數據類型的數組轉化為其包裝類型的list,可以使用guava類庫的工具方法,示例如下:

1
2
int [] intArray = { 1 2 3 4 };
List<Integer> list = Ints.asList(intArray);

 

 二、asList方法返回的是數組的一個視圖

視圖意味着,對這個list的操作都會反映在原數組上,而且這個list是定長的,不支持add、remove等改變長度的方法。

1
2
3
4
5
6
7
8
9
10
11
public  class  App {
   public  static  void  main(String[] args) {
     int [] intArray = { 1 2 3 4 };
     List<Integer> list = Ints.asList(intArray);
     list.set( 0 100 );
     System.out.println(Arrays.toString(intArray));
     list.add( 5 );
     list.remove( 0 );
 
   }
}

 output:

1
2
3
[ 100 2 3 4 ]
UnsupportedOperationException
UnsupportedOperationException


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM