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

慕哥6287543
TA贡献1831条经验 获得超10个赞
将逗号分隔的字符串转换为列表
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>
.

素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
String [] items = commaSeparated.split("\\s*,\\s*");
List<String> container = Arrays.asList(items);
添加回答
举报
0/150
提交
取消