Java中方法及值的不同层次使用

今天在学习Java的二维数组,做了这样一个小练习,用来按要求分割字符串转化为Double数组

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();
}
}
}


对于二位数株d[][]而言出现了不同层次length的用法:
d.length;
d[i].length;

我习惯了c语言的思维,想了很久d的二维长度求法,同时也没注意到String[] sSecond在for循环中的次次更新。以后要注意Java中方法的使用不同于c语言中的函数,Java的方法可以在对象的不同层次共用。

原文地址:https://www.cnblogs.com/Sherlock-J/p/12926088.html