public class ArrayParser {
public static void main(String[] args) {
double[][] d;
String s = "1,2;3,4,5;6,7,8";
String[] sFirst = s.split(";");
d = new double[sFirst.length][];
for(int i=0; i<sFirst.length; i++) {
String[] sSecond = sFirst[i].split(",");
for(int j=0; j<sSecond.length; j++) {
d[i] = new double[sSecond.length];
d[i][j] = Double.parseDouble(sSecond[j]);
}
}
for(int i=0; i<d.length; i++) {
for(int j=0; j<d[i].length; j++) {
System.out.print(d[i][j] + " ");
}
System.out.println();
}
}
}为什么输出结果为
0.0 2.0
0.0 0.0 5.0
0.0 0.0 8.0
本人认为结果为
1.0 2.0
3.0 4.0 5.0
6.0 7.0 8.0
请大家为小弟指点一下,本人刚学JAVA不久,谢谢!
public static void main(String[] args) {
double[][] d;
String s = "1,2;3,4,5;6,7,8";
String[] sFirst = s.split(";");
d = new double[sFirst.length][];
for(int i=0; i<sFirst.length; i++) {
String[] sSecond = sFirst[i].split(",");
for(int j=0; j<sSecond.length; j++) {
d[i] = new double[sSecond.length];
d[i][j] = Double.parseDouble(sSecond[j]);
}
}
for(int i=0; i<d.length; i++) {
for(int j=0; j<d[i].length; j++) {
System.out.print(d[i][j] + " ");
}
System.out.println();
}
}
}为什么输出结果为
0.0 2.0
0.0 0.0 5.0
0.0 0.0 8.0
本人认为结果为
1.0 2.0
3.0 4.0 5.0
6.0 7.0 8.0
请大家为小弟指点一下,本人刚学JAVA不久,谢谢!
public class ArrayParser
{
public static void main(String[] args)
{
double[][] d;
String s = "1,2;3,4,5;6,7,8";
String[] sFirst = s.split(";");
d = new double[sFirst.length][];
for(int i=0; i<sFirst.length; i++)
{
String[] sSecond = sFirst[i].split(",");
d[i] = new double[sSecond.length]; //这一句放在这
for(int j=0; j<sSecond.length; j++)
{
d[i][j] = Double.parseDouble(sSecond[j]);
}
}
for(int i=0; i<d.length; i++)
{
for(int j=0; j<d[i].length; j++)
{
System.out.print(d[i][j] + " ");
}
System.out.println();
}
}
}
public class ArrayParser
{
public static void main(String[] args)
{
double[][] d;
String s = "1,2;3,4,5;6,7,8";
String[] sFirst = s.split(";");
d = new double[sFirst.length][];
for(int i=0; i<sFirst.length; i++)
{
String[] sSecond = sFirst[i].split(",");
d[i] = new double[sSecond.length]; //这一句放在这************
for(int j=0; j<sSecond.length; j++)
{
d[i][j] = Double.parseDouble(sSecond[j]);
}
}
for(int i=0; i<d.length; i++)
{
for(int j=0; j<d[i].length; j++)
{
System.out.print(d[i][j] + " ");
}
System.out.println();
}
}
}