`
huntersp
  • 浏览: 40199 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

List与Array之间的转换

    博客分类:
  • JAVA
阅读更多
★ 数组转Collection(浅转换)
用JDK中的java.util.Arrays类
import java.util.Arrays;   
  
String[] strArray = {"aaa", "bbb", "ccc"};   
List strList = Arrays.asList(strArray);  
不过Arrays.asList()方法返回的List不能add,remove对象,因为该方法的实现是使用参数引用的数组的大小来new的一个ArrayList,对返回列表的更改会“直写”到数组,而数组没有add,remove方法。



★ 数组转Collection(深转换)
使用Apache Jakarta Commons Collections包

import org.apache.commons.collections.CollectionUtils;   
  
String[] strArray = {"aaa", "bbb", "ccc"};   
List strList = new ArrayList();   
Set strSet = new HashSet();   
CollectionUtils.addAll(strList, strArray);   
CollectionUtils.addAll(strSet, strArray);
将数组完全转换成一个任意类型的Collection
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics