如何将String转换为ArrayList?

编程入门 行业动态 更新时间:2024-10-15 06:19:27
本文介绍了如何将String转换为ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

In my String, I can have an arbitrary number of words which are comma separated. I wanted each word added into an ArrayList. E.g.:

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

推荐答案

尝试类似

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

  • Arrays.asList documentation
  • String.split 文档
  • ArrayList(Collection) 构造函数文档
    • Arrays.asList documentation
    • String.split documentation
    • ArrayList(Collection) constructor documentation
    • 演示:

      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]

      此帖已被重写为此处。

更多推荐

如何将String转换为ArrayList?

本文发布于:2023-06-08 21:32:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/589439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   如何将   String   ArrayList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!