如何将String转换为ArrayList? [英] How to convert a String into an ArrayList?

查看:647
本文介绍了如何将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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆