Java:返回⼆维数组中最⼤值及下标二维数组下标怎么理解
设计⼀个名为Location的类,定位⼆维数组中的最⼤值及其位置。这个类包括公共的数据域row、column和maxValue,⼆维数组中的最⼤值及其下标⽤int型的row和column以及double型的maxValue存储。编写下⾯的⽅法,返回⼀个⼆维数组中最⼤值的位置。
public static Location locateLargest(double[][] a)
返回值是⼀个Location的实例。
贴代码
import java.util.Scanner;
public class Exercise8_13 {
public static void main(String[] args) {
System.out.println("Enter the number of rows and colums of the array :");
Scanner input = new Scanner(System.in);
int row = Int();
int column = Int();
System.out.println("Enter the array");
double[][] array = new double[row][column];
for(int i=0 ;i < array.length;i++){
for(int j=0 ;j <array[i].length ;j++ ){
array[i][j] = Double();
}
}
Location  location1=locateLargest(array);
System.out.println("The location of the largets element is "+location1.maxValue+"at "+"("+w+","+lumn+")");
}
public static Location locateLargest(double[][] a) {
Location location = new Location();
int row = 0;
int column = 0;
double maxValue = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if(a[i][j]>maxValue){
maxValue=a[i][j];
row =i;
column=j;
}
}
}
location.maxValue = maxValue;
return location;//实例
}
}
class Location{
public int row;
public int column;
public double maxValue;
}