3. 编写一个程序,返回一个 double 类型的二维数组,数组中的元素通过解析字符串参数获得,如字符串参数:“1,2;3,4,5;6,7,8”,则对应的数组为:

 d[0][0]=1.0, d[0][1]=2.0

   d[1][0]=3.0, d[1][1]=4.0, d[1][2]=5.0

   d[2][0]=6.0,d[2][1]=7.0, d[2][2]=8.0

java代码如下:

public class Test {
	public static void main(String[] args) {
		String s = "1,2;3,4,5;6,7,8";
		double d[][];
		String first[] = s.split(";");
		d = new double[first.length][];
		for(int i=0;i<first.length;i++){
			String second[] = first[i].split(",");
			d[i] = new double[second.length];
			for(int j=0;j<second.length;j++){
				d[i][j] = Double.parseDouble(second[j]); 
			}
		}
		for (int i = 0; i < d.length; i++) {
			for (int j = 0; j < d[i].length; j++) {
				System.out.print("d[" + i + "][" + j + "] = " + d[i][j] + "  ");
			}
			System.out.println();
		}
	}
}

  运行结果:

d[0][0] = 1.0 d[0][1] = 2.0
d[1][0] = 3.0 d[1][1] = 4.0 d[1][2] = 5.0
d[2][0] = 6.0 d[2][1] = 7.0 d[2][2] = 8.0

posted on 2017-04-25 10:56  牧羊人的世界  阅读(147)  评论(0编辑  收藏  举报