2009-01-19 163 views

回答

23

很容易只是做自己:

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; 
    } 
} 
0

您还可以使用分流/从阿帕奇百科全书StringUtils

1

的样本加入与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);