反(解析出)的阵列。toString(int[])[重复]
-
19-08-2019 - |
题
这个问题已经有一个答案在这里:
- 转换串int列在java 9答案
有没有在JAVA或雅加达共同(或任何其他地方)的一个方法,可以分析出阵列。toString,至少对于整数阵?
int[] i = fromString(Arrays.toString(new int[] { 1, 2, 3} );
解决方案
很容易只是做它自己:
public class Test {
public static void main(String args[]){
int[] i = fromString(Arrays.toString(new int[] { 1, 2, 3} ));
}
private static int[] fromString(String string) {
String[] strings = string.replace("[", "").replace("]", "").split(", ");
int result[] = new int[strings.length];
for (int i = 0; i < result.length; i++) {
result[i] = Integer.parseInt(strings[i]);
}
return result;
}
}
其他提示
一个样本与fastjson,JSON图书馆:
String s = Arrays.toString(new int[] { 1, 2, 3 });
Integer[] result = ((JSONArray) JSONArray.parse(s)).toArray(new Integer[] {});
另一样本与番石榴:
String s = Arrays.toString(new int[] { 1, 2, 3 });
Iterable<String> i = Splitter.on(",")
.trimResults(CharMatcher.WHITESPACE.or(CharMatcher.anyOf("[]"))).split(s);
Integer[] result = FluentIterable.from(i).transform(Ints.stringConverter())
.toArray(Integer.class);
你也可以使用分裂/加入从阿帕奇景 既
不隶属于 StackOverflow