反转(解析输出)的 Arrays.toString(int[]) [英] Reverse (parse the output) of Arrays.toString(int[])

查看:20
本文介绍了反转(解析输出)的 Arrays.toString(int[])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JDK 或 Jakarta Commons(或其他任何地方)中是否有一种方法可以解析 Arrays.toString 的输出,至少对于整数数组?

Is there in the JDK or Jakarta Commons (or anywhere else) a method that can parse the output of Arrays.toString, at least for integer arrays?

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

这篇关于反转(解析输出)的 Arrays.toString(int[])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆