日期:2014-05-20  浏览次数:20908 次

求解!
public class CopyArray {
public static void main(String args[]) {
double b[][][];
String s = "{(1,2;3,4;)}{(5,6;7,8;)}{(9,10;11,12)}";
String sFirst[] = s.split("{}");
b = new double[sFirst.length][][];
for (int i = 0; i < sFirst.length; i++) {
  String sSecond[] = sFirst[i].split("()");
b[i] = new double[sSecond.length][];
for (int j = 0; j < sSecond.length; j++) {
String sThirth[] = sSecond[j].split(";");
b[j] = new double[sThirth.length][];
for (int k = 0; k < sThirth.length; k++) {
String sFourth[] = sThirth[k].split(",");
b[i][j][k] = Double.parseDouble(sSecond[k]);
}

}
}
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
for (int k = 0; k < b[j].length; k++) {

System.out.print(b[i][j][k] + " ");
}
}
System.out.println();

}

}
}
我的本意是想把这个字符串变成一个三维数组,可是一运行有异常,从未见过的异常,本人菜鸟一只!但很想学习!承蒙各位高手不吝赐教!

------解决方案--------------------
都是最基本的错误,LZ要在基础上多下功夫啊
Java code
double b[][][];
String s = "{(1,2;3,4;)}{(5,6;7,8;)}{(9,10;11,12)}";
String sFirst[] = s.split("[}][{]"); //修改
b = new double[sFirst.length][][];
for (int i = 0; i < sFirst.length; i++) {
    String sSecond[] = sFirst[i].replaceAll("[{}()]", "").split(";"); //修改
    b[i] = new double[sSecond.length][];
    for (int j = 0; j < sSecond.length; j++) {
        String sThirth[] = sSecond[j].split(",");
        b[i][j] = new double[sThirth.length]; //修改
        for (int k = 0; k < sThirth.length; k++) {
            //String sFourth[] = sThirth[k].split(","); //修改
            b[i][j][k] = Double.parseDouble(sThirth[k]);//修改
        }
    }
}

for (int i = 0; i < b.length; i++) {
    for (int j = 0; j < b[i].length; j++) {
        for (int k = 0; k < b[i][j].length; k++) { //修改

            System.out.print(b[i][j][k] + " ");
        }
    }
    System.out.println();
}

------解决方案--------------------
出错很正常,刚开始学都差不多
多花点心思在基础语法上
------解决方案--------------------
先把一维数组搞透了,再去弄二维数组。三维数组在面向对象程序中很少能用到,了解一下就可以了。

但是,一维数组、二维数组必须要练得非常熟练

------解决方案--------------------
不熟二维数组 。。。提醒了