Java集合与数组的互转

时间:2022-09-03 16:56:40

在实际工作中,我们经常需要Collection类型与数组的互相转换,Java API也提供了相应的方法帮我们完成操作。

集合转为数组
java.util.Collection.toArray(T[] a)
数组转为集合
java.util.Arrays.toList(T…a)

集合转数组

  • 源码
@Test
public void testList2Array() {

    // 构建一个集合
    List<String> list = new ArrayList<>();
    list.add("Calligraphy is the art of beautiful handwriting.");
    list.add("He really takes good care of his sister.What a thoughtful boy!");
    list.add("Working hard in the gymnasium keeps us fit.");

    // 转为数组
    String[] strings = list.toArray(new String[list.size()]);

    // 遍历
    Arrays.stream(strings).forEach(System.out::println);
}
  • 运行结果
Calligraphy is the art of beautiful handwriting.
He really takes good care of his sister.What a thoughtful boy!
Working hard in the gymnasium keeps us fit.

数组转集合

  • 源码
//数组转化为集合
 @Test
 public void array2List() {
     String[]arr = new String[]{"Calligraphy is the art of beautiful handwriting."
             ,"He really takes good care of his sister.What a thoughtful boy!"
             ,"Working hard in the gymnasium keeps us fit."};

     // 转为集合
     List<String> list = Arrays.asList(arr);

     // 遍历
     list.forEach(n -> System.out.println(n));  
 }
  • 运行结果
Calligraphy is the art of beautiful handwriting.
He really takes good care of his sister.What a thoughtful boy!
Working hard in the gymnasium keeps us fit.

数组转集合使用的是Arrays.asList(T…a)方法。这里特别需要注意的是,使用这种方式转来的list的类型是Arrays的一个内部类【即java.util.Arrays.ArrayList】,拥有的方法数量有限,不具备add 、remove等的常用操作。

如下操作:

//数组转化为集合,并尝试添加
 @Test
 public void array2ListAttemptAdd() {
     String[]arr = new String[]{"Calligraphy is the art of beautiful handwriting."
             ,"He really takes good care of his sister.What a thoughtful boy!"
             ,"Working hard in the gymnasium keeps us fit."};

     // 转为集合
     List<String> list = Arrays.asList(arr);

     try {
        //会报java.lang.UnsupportedOperationException
         list.add("When it gets cold, I sleep with a thick quilt to stay warm.");
    } catch (Exception e) {
        e.printStackTrace();
    }

     // 遍历
     list.forEach(n -> System.out.println(n));  
 }

若要经转化后有增加删除等操作,可转为ArrayList或其他拥有完整操作的List类

  • 源码
 //1. 数组转化为集合
 //2. 并将该结果集合转化为java.util.ArrayList【严格规范的做法】
    //3. 然后尝试添加
 @Test
 public void array2ArrayListAndAdd() {
     String[]arr = new String[]{"Calligraphy is the art of beautiful handwriting."
             ,"He really takes good care of his sister.What a thoughtful boy!"
             ,"Working hard in the gymnasium keeps us fit."};

     // 转为集合
     // 也可以是java.util.LinkedList、java.util.Vector<E>
     List<String> list = new java.util.ArrayList<>(Arrays.asList(arr));

     try {
        //正常添加
         list.add("When it gets cold, I sleep with a thick quilt to stay warm.");
    } catch (Exception e) {
        e.printStackTrace();
    }

     // 遍历
     list.forEach(n -> System.out.println(n));  
 }
  • 运行结果
Calligraphy is the art of beautiful handwriting.
He really takes good care of his sister.What a thoughtful boy!
Working hard in the gymnasium keeps us fit.
When it gets cold, I sleep with a thick quilt to stay warm.

参考文档:
1. https://blog.csdn.net/wthfeng/article/details/61416464