温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

String和List<String>间的相互转换

发布时间:2023-05-26 10:09:38 来源:亿速云 阅读:127 作者:栢白 栏目:开发技术

本篇文章和大家了解一下String和List<String>间的相互转换。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

目录
  • String和List<String>间相互转换

  • List转String的简单方法

  • String[]和List<String>的区别

    • 两者的区别

  • 总结

    String和List<String>间相互转换

        public void test() {
            //字符串转list<String>
            String str = "asdfghjkl";
            List<String> lis = Arrays.asList(str.split(""));
            for (String string : lis) {
                System.out.println(string);
            }
            //list<String>转字符串
            System.out.println(String.join("", lis));
        }

    List转String的简单方法

    import org.apache.commons.lang.StringUtils;
    public static void main(String[] args) {
            List list = new ArrayList<>(); 
            list.add("a");//String类型
            list.add("bb");
            list.add(1);//int类型 
            String s = StringUtils.join(list,",");
            System.out.println(s);
    }

    输出:a,bb,1

    注意需要引入:org.apache.commons.lang.StringUtils 包

    String[]和List<String>的区别

    两者的区别

    结构方面:

    List< String >:泛型,非定长,可变。

    String和List<String>间的相互转换

    String[]:数组,定长,不可变。

    String和List<String>间的相互转换

    使用方面:

    他们的作用一样,但是灵活性不一样。

    List< String >是可以方便使用的,如果不能确定数组的长度,或者需要不断的像中间插入一个字符串,可以用List< String >。

    String[]是定长的,如果能确定字符串数组的长度,可以使用String[]。

    以上就是String和List<String>间的相互转换的简略介绍,当然详细使用上面的不同还得要大家自己使用过才领会。如果想了解更多,欢迎关注亿速云行业资讯频道哦!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI