1 .使用本机方法拆分数组并将其添加到List中
liststringresultlist=new ArrayList (array.length ); for(strings:Array ) resultlist.add ); }2.使用arrays.as list ()
liststringresultlist=new ArrayList (Arrays.as list ) array ); 注意:如果调用Arrays.asList (),则返回类型为ArrayList,但此ArrayList是Array的内部类,add ) )将报告错误。 Java.lang.unsupportedoperationexcepepppt
使用Collections.addAll (
liststringresultlist=new ArrayList (array.length ); collections.addall(resultlist,array ); 使用List.of (
此方法是Java9的新方法,在List接口中定义,是静态方法,因此可以直接从类名调用。
liststringresultlist=list.of (array ); 3359 blog.csdn.net/QQ _ 43437465/article/details/89344983
分裂方法
string strtest=" 1,2,";
String arry[]=strtest.split (“,”);
末尾的空字符串已被丢弃!
另一种重载方法(public string [ ] split (string regex,int limit ) ) ) ) ) ) ) )。
由于limit参数控制模式的应用次数,因此会影响数组的生成长度。 如果此限制n大于0,则模式最多应用n - 1次,数组的长度不应大于n,数组的最后一个条目包含超过最后匹配分隔符的所有输入。 如果n不为正,模式将应用尽可能多的次数,并且数组可以是任意长度。 如果n为0,则模式将应用尽可能多的次数,数组可以是任意长度,并丢弃末尾的空字符串。
对于函数公共字符串[ ] split (字符串注册)
此方法的目的是使用给定的表达式和限制参数0调用两个参数split方法。 因此,结果数组不包含末尾的空字符串
所以,如果你不想舍弃结尾的空字符串,你应该这样写:
string strtest=" 1,2,";
String arry[]=strtest.split (“,”,-1);