Java 将列表转换为字符串,然后再转换回列表

Java 将列表转换为字符串,然后再转换回列表,java,arrays,list,Java,Arrays,List,如何将列表转换为字符串数组并返回,同时保留[和] 例如: [hello, hello2, hello3] 列表->字符串 Arrays.toString(list.toArray()) 字符串->列表 List<String> converted = Arrays.asList(str.substring(1, str.length()-1).split(",")); 如果我没弄错你想要什么 对于要字符串的列表: String str = Arrays.to

如何将列表转换为字符串数组并返回,同时保留[和]

例如:

[hello, hello2, hello3]
列表->字符串

Arrays.toString(list.toArray())
字符串->列表

List<String> converted = Arrays.asList(str.substring(1, str.length()-1).split(","));

如果我没弄错你想要什么

对于要字符串的列表:

String str = Arrays.toString(list.toArray());
对于要列出的字符串:

List<String> list = Arrays.asList(str.substring(1, str.length()-1).split(","));

现在使用以下命令创建一个字符串:

字符串结果=console.stream .mapn->String.valueOfn
.collectors.joining,,{,} 有很多方法,这里有一种

List<String> list =
        Arrays.asList("hello", "hello2", "hello3");

String s = list.toString();
System.out.println(s);

list = Arrays.asList(s.substring(1,s.length()-1).split("\\s*,\\s*"));
System.out.println(list);

问题是,我的列表包含文件的最后几行,所以对于您的解决方案,我也没有括号,这些行都在一行中line@Givou没有括号是什么意思?当我测试代码时,它包括[和],嗯?好吧,那是我的错,我把括号换成了零。我需要这个,因为我通过套接字传输日志的最后10行。当我将字符串转换回接收端时,我现在得到[[Bla]blabli,blabli]@Givou我已经修复了它。如果有帮助,请考虑接受!看起来您正在尝试重新创建序列化。如果要将对象序列化为人类可读窗体,请考虑使用JSON格式。关于这个主题有很多例子。请写下你想要实现的输入和输出编辑你的文章。[你好,hello2,hello3]-是输入还是输出?例如,我有:。。。。。我想。。。。。。
[hello, hello2, hello3]
[hello, hello2, hello3]