您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java中如何将数组转换为List?

2024/8/1 18:34:24发布337次查看
1、使用原生模式,拆分数组,添加到list中 
list resultlist = new arraylist<>(array.length);for (string s : array) {resultlist.add(s);}
2、使用arrays.aslist()
listresultlist=newarraylist<>(arrays.aslist);
注:当调用arrays.aslist()时,它的返回值类型是arraylist,但是这个arraylist是array的内部类别,当调用add()时,它会报错:java.lang.unsupportedoperationexception,结果会因array的某一值而改变,因此需要重新构建一个新的arraylist。
3、使用collections.addall()
private void testarraycasttolistefficient(){  string[] strarray = new string[2];  arraylist< string> arraylist = new arraylist<string>(strarray.length);  collections.addall(arraylist, strarray);  arraylist.add(1);  system.out.println(arraylist); }
4、使用list.of()
list resultlist = list.of(array);
这个方法是java9的新方法,定义在list接口中,是静态方法,可以直接调用类名。
以上就是java中如何将数组转换为list?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product