java 将字符串转换为列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37745244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:45:02  来源:igfitidea点击:

Convert String into List

java

提问by Peter Penzov

I have List which I use to convert into String:

我有用于转换为字符串的列表:

    private String convertList(List<String> list)
    {
        String listString = "";

        for (String s : list)
        {
            listString += s + ",";
        }
        return listString;
    }

But I also want to implement the same operation backwards. I want to generate List from String using ,as delimiter. How this can be implemented?

但我也想反向实现相同的操作。我想使用,作为分隔符的字符串生成列表。如何实施?

回答by Arnaud

Just use splitto generate an array of Stringtokens, and Arrays.asListto generate the List:

只需用于split生成String令牌数组,并Arrays.asList生成List

List<String> theList = Arrays.asList(bigString.split(","));