public class StringToDouble {
public static void main(String []args) {
String s = "1,2;3,4,5;6,7,8";
double[][] array =StringToDouble.f(s);
for (int i =0;i<array.length;i++) {
for (int j = 0;j<array[i].length;j++) {
System.out.println(array[i][j]);
}
}
}
public static double[][] f(String s) {
String [] s1 = s.split(";");
double[][] d = new double [s1.length][];
for (int i = 0;i<s1.length;i++) {
String [] s2 = s1[i].split(",");
for (int j = 0;j<s2.length;j++) {
d[i][j] = Double.parseDouble(s2[j]);
}
}
return d;
}
}
将一个字符串转换为double型二维数组,编译通过了,运行的时候提示nullPointerException。java提示错误发生在第4行和第21行。