您的位置:首页 > 编程语言 > Java开发

Java:<返回二维数组中最大值及下标>

2017-09-30 00:19 731 查看
设计一个名为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 = input.nextInt();
int column = input.nextInt();
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] = input.nextDouble();
}
}
Location  location1=locateLargest(array);
System.out.println("The location of the largets element is "+location1.maxValue+"at "+"("+location1.row+","+location1.column+")");
}
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.row = row;
location.column = column;
location.maxValue = maxValue;
return location;//实例
}
}
class Location{
public  int row;
public int column;
public  double maxValue;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐