转换ArrayList.toString()返回的ArrayList中的一个呼叫 [英] convert ArrayList.toString() back to ArrayList in one call
问题描述
我有一个的toString()
重新presentation一个的ArrayList
。
复制的toString()
值到剪贴板,我要回去将其复制到我的IDE编辑器,并创建的ArrayList
实例一行。其实,我真正做的是这样的:
- 我ArrayList.toString()有数据我需要安装一个单元测试。
- 我想这ArrayList.toString()复制到我的编辑器来构建对这种边缘情况的测试
- 我不想来解析手动什么
我的输入是这样的:
[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]
以下不工作:
-
Arrays.asList()
- 谷歌集合
Lists.newArrayList()
建议?
<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/String.html#substring%28int,%20int%29\">Substring牙套了,<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29\">split它,
(逗号和空格),最后其提供给阵列#asList()
。
一个String =[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];
清单&LT;串GT;列表= Arrays.asList(s.substring(1,s.length() - 1).split(,));
请注意,这将在您的特定情况下工作,但不是在所有情况下。你可以例如具有一个字符串列表,其中至少一个含有一后续逗号和空间。然后,分裂会失败。
I have a toString()
representation of an ArrayList
.
Copying the toString()
value to clipboard, I want to copy it back into my IDE editor, and create the ArrayList
instance in one line. In fact, what I'm really doing is this:
- 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
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()
Suggestions?
Substring the braces away, split it on ,
(comma and space) and finally feed it to 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.
这篇关于转换ArrayList.toString()返回的ArrayList中的一个呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!