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

来源:互联网 发布:mysql更新一条数据 编辑:程序博客网 时间:2024/06/05 23:42

设计一个名为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;    }
阅读全文
0 0
原创粉丝点击