这个public static Location
locateLargest(double[][] a) 这个我居然没看懂他是一个返回值为Location类的方法,我还在疑惑咋这个方法的名字是个分段的。。。请教了其他人才知道返回Location这个类, 而且这个方法是static,不用创建实例就可以引用,十分方便。
package Experiment2;
public class Location {
public int row, col;
public double maxValue;
Location() {
}
Location(int r, int c, double max) {
this.row = r;
this.col = c;
this.maxValue = max;
}
public static Location locateLargest(double[][] a) {
double max = a[0][0];
int r,c;
r = c = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] > max) {
max = a[i][j];
r = i;
c = j;
}
}
}
Location loc = new Location(r, c, max);
return loc;
}
}
测试类
package Experiment2;
public class Test_Location {
public static void main(String[] args) {
double[][] a = {{1, 2, 3}, {8, 9, 9, 5}, {4, 3, 5, 7, 8}};
Location test = new Location();
test = Location.locateLargest(a);
System.out.println("最大元素是:" +
test.maxValue);
System.out.println("位置是:(" + test.row
+ "," + test.col + ")");
}
}