在一次调用中将ArrayList.toString()转换回ArrayList [英] convert ArrayList.toString() back to ArrayList in one call
问题描述
我有一个 toString()
表示 ArrayList
。
将 toString()
值复制到剪贴板,我想将它复制回我的IDE编辑器,并创建 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:
- 我的ArrayList.toString()有我需要设置单元测试的数据。
- 我想将此ArrayList.toString()复制到我的编辑器中,以构建针对此边缘情况的测试
- 我不想解析
- 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
以下不工作:
-
Arrays.asList()
- google collections
Lists.newArrayList()
Arrays.asList()
- google collections
Lists.newArrayList()
建议?
推荐答案
Substring 大括号, / / docs / api / java / lang / String.html#split%28java.lang.String%29rel =nofollow noreferrer / code>(逗号和空格),最后将其提供给 Arrays#asList()
。
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屋!