java 一次调用将 ArrayList.toString() 转换回 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2774142/
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
convert ArrayList.toString() back to ArrayList in one call
提问by dotnetnewbie
I have a toString()representation of an ArrayList.
我有一个toString()的表示ArrayList。
Copying the toString()value to clipboard, I want to copy it back into my IDE editor, and create the ArrayListinstance in one line. In fact, what I'm really doing is this:
将toString()值复制到剪贴板,我想将它复制回我的 IDE 编辑器,并ArrayList在一行中创建实例。事实上,我真正在做的是:
- my ArrayList.toString() has data I need to setup a unit test.
- I want to copy this ArrayList.toString() into my editor to build a test against this edge case
- I don't want to parse anything by hand
- 我的 ArrayList.toString() 有我需要设置单元测试的数据。
- 我想将此 ArrayList.toString() 复制到我的编辑器中以针对此边缘情况构建测试
- 我不想手动解析任何东西
My input looks like this:
我的输入如下所示:
[15.82, 15.870000000000001, 15.92, 16.32, 16.32, 16.32, 16.32, 17.05, 17.05, 17.05, 17.05, 18.29, 18.29, 19.16]
The following do not work:
以下不工作:
Arrays.asList()- google collections
Lists.newArrayList()
Arrays.asList()- 谷歌收藏
Lists.newArrayList()
Suggestions?
建议?
回答by BalusC
Substringthe braces away, splitit on ,(comma and space) and finally feed it to Arrays#asList().
将大括号子串起来,将其拆分,(逗号和空格),最后将其提供给Arrays#asList().
String s = "[15.82, 15.870000000000001, 15.92, 16.32, 16.32, 16.32, 16.32, 17.05, 17.05, 17.05, 17.05, 18.29, 18.29, 19.16]";
List<String> list = Arrays.asList(s.substring(1, s.length() - 1).split(", "));
Note that this will work in your particular case, but not in all circumstances. You may for example have a list of strings of which at least one contains a subsequent comma and space. The split would then fail.
请注意,这适用于您的特定情况,但并非适用于所有情况。例如,您可能有一个字符串列表,其中至少有一个包含后续逗号和空格。拆分将失败。
回答by Joachim Sauer
Generally speaking the toString()of any objects does not contain information to reproduce the original object without any further information.
一般来说toString(),任何对象的 都不包含复制原始对象的信息而没有任何进一步的信息。
In your specific case the example could be produced by many different ArrayListinstances (as well as pretty much all other Listimplementations which have identical toString()) implementations.
在您的特定情况下,该示例可以由许多不同的ArrayList实例(以及几乎所有List具有相同的其他实现toString())实现生成。
As an extreme example, think of an ArrayListthat contains a single element which is the Stringwith the content 15.82, 15.870000000000001, 15.92, 16.32, 16.32, 16.32, 16.32, 17.05, 17.05, 17.05, 17.05, 18.29, 18.29, 19.16. That ArrayListwould produce the exact same output as your original ArrayList. And since two different inputs produce the same output, there's no way this function can be reversed without additional information.
作为一个极端的例子,考虑一个ArrayList包含单个元素的String与 content 15.82, 15.870000000000001, 15.92, 16.32, 16.32, 16.32, 16.32, 17.05, 17.05, 17.05, 17.05, 18.29, 18.29, 19.16。这ArrayList将产生与原始ArrayList. 而且由于两个不同的输入产生相同的输出,如果没有额外的信息,这个函数是不可能反转的。
If, however, we have additional information, such as the content type of the original ArrayList, then it becomes possible in some cases. If we know that all elements of the Listwere of type Double, then it's actually pretty easy:
但是,如果我们有其他信息,例如原始 的内容类型ArrayList,则在某些情况下成为可能。如果我们知道 的所有元素List都是 type Double,那么实际上很简单:
public static List<Double> stringToList(final String input) {
String[] elements = input.substring(1, input.length() - 1).split(", ");
List<Double> result = new ArrayList<Double>(elements.length);
for (String item : elements) {
result.add(Double.valueOf(item));
}
return result;
}
Granted, it's not a one-liner, but it's not too bad.
当然,它不是单线,但也不算太糟糕。
回答by dotnetnewbie
this works, but perhaps someone has something more elegant?
这有效,但也许有人有更优雅的东西?
List<String> list = Lists.newArrayList(Splitter.on(",").omitEmptyStrings().split("JUN10, SEP10, DEC10, MAR11, JUN11, SEP11, DEC11, MAR12, JUN12, SEP12, DEC12, MAR13, DEC13, DEC14"));
assertEquals(14, list.size());
回答by ring bearer
I want to copy this ArrayList.toString() into my editor to build a test against this edge case
我想将此 ArrayList.toString() 复制到我的编辑器中以针对此边缘情况构建测试
If at all you are copying, can't you just copy values omitting the square brackets and call an
如果你在复制,你不能只是复制省略方括号的值并调用
Arrays.asList(...) ?
回答by ILMTitan
If you are just looking for a copy paste solution,
如果您只是在寻找复制粘贴解决方案,
Arrays.asList(15.82, 15.870000000000001, 15.92, 16.32, 16.32, 16.32, 16.32, 17.05, 17.05, 17.05, 17.05, 18.29, 18.29, 19.16);
i.e. delete the quotes and square brackets before trying to put it into asList(). That should give you a List which you could easily use to create a new ArrayList.
即在尝试将其放入 asList() 之前删除引号和方括号。这应该为您提供一个列表,您可以轻松地使用它来创建新的 ArrayList。
If the data are more complicated than doubles, you may have to find a way to parse them.
如果数据比 double 更复杂,您可能需要找到一种方法来解析它们。
回答by Ophidian
The "must be one line" requirement pretty much ensures that you're going to get some ugly mess of chained API calls unless you write a helper method to encapsulate things.
“必须是一行”的要求几乎确保您将得到一些糟糕的链式 API 调用混乱,除非您编写一个辅助方法来封装事物。
It's kind of ugly, but similar to dotnetnewbie's approach:
这有点难看,但类似于 dotnetnewbie 的方法:
List<String> result = Arrays.asList(listString.substring(1, listString.length()-1).split(",\s*"));
It's off the cuff so I'm sure that can be cleaned up a fair bit.
它不在袖口上,所以我相信它可以稍微清理一下。
A more elegant and easier to read approach would definitely be to either break it out over a couple of lines or refactor to a helper method in your test case (or a Util class) that you can call with your string and have it return the output that you'd like.
一种更优雅、更易于阅读的方法肯定是将其分解为几行或重构为测试用例(或 Util 类)中的辅助方法,您可以使用字符串调用该方法并让它返回输出你想要的。

