Java集合与数组的互转

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

  • 集合转为数组
    java.util.Collection.toArray(T[] a)
  • 数组转为集合
    (不推荐,不支持集合的add()、remove()、clear()方法)java.util.Arrays#asList(T… a)
    (推荐,新建List)List list = new ArrayList<>(Arrays.asList(“a”, “b”, “c”))

集合转数组

  • 源码
@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()]);

	// 遍历(JDK8)
	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
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值