如何将字符串转换为数组列表?

2022-08-31 08:16:22

在我的字符串中,我可以有任意数量的以逗号分隔的单词。我希望将每个单词添加到 ArrayList 中。例如:

String s = "a,b,c,d,e,.........";

答案 1

试试类似的东西

List<String> myList = new ArrayList<String>(Arrays.asList(s.split(",")));

演示:

String s = "lorem,ipsum,dolor,sit,amet";

List<String> myList = new ArrayList<String>(Arrays.asList(s.split(",")));

System.out.println(myList);  // prints [lorem, ipsum, dolor, sit, amet]

这篇文章已经重写为一篇文章在这里


答案 2
 String s1="[a,b,c,d]";
 String replace = s1.replace("[","");
 System.out.println(replace);
 String replace1 = replace.replace("]","");
 System.out.println(replace1);
 List<String> myList = new ArrayList<String>(Arrays.asList(replace1.split(",")));
 System.out.println(myList.toString());