如何将逗号分隔的字符串转换为ArrayList?在Java中是否有任何内置方法允许我们将逗号分隔的字符串转换为某个容器(例如数组、列表或向量)?或者我需要为此编写自定义代码吗?String commaSeparated = "item1 , item2 , item3";ArrayList<String> items = //method that converts above string into list??
3 回答

Qyouu
TA贡献1786条经验 获得超11个赞
将逗号分隔的字符串转换为列表
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));
zero or more whitespace, a literal comma, zero or more whitespace
.remove()
List
ArrayList
new ArrayList<String>
.

jeck猫
TA贡献1909条经验 获得超7个赞
String [] items = commaSeparated.split("\\s*,\\s*");
List<String> container = Arrays.asList(items);
添加回答
举报
0/150
提交
取消