java中怎么将由数字组成的字符串转化为数组?

public class StringToArray {
  public static void main(String[] args) {
  String str = "12,45,56,89";//你给的字符串
  String[] strArray = str.split(",");//使用split方法根据逗号将字符串拆分成字符串数组
  int[] num = new int[strArray.length];//定义整型数组用来接收转换的字符串数组
  //将字符串数组转换成整型数组
  for (int i = 0; i < strArray.length; i++) {
  num[i] = Integer.parseInt(strArray[i]);
  }
  System.out.println("输出整型数组:");
  for (int i = 0; i < num.length; i++) {
  System.out.print(num[i] + " ");
  }
  }
  }

你可能感兴趣的:(java)