java封装一个类MatrixLocation, 查询二维数组中的最大值及其位置。

软件NetBeans IDE 7.0.1,需要单独写主类。

封装一个类MatrixLocation, 查询二维数组中的最大值及其位置。最大值用double类型的maxValue存储,位置用int类型的row和column存储。封装执行主类,给定二维数组,输出最大值及其位置。

主类:

            int [][] a={{2,3,4},{5,6,7}};
            MatrixLocation m=new MatrixLocation();
            m.MatrixMax(a);

MatrixLocation类:

public class MatrixLocation {
    public int[][] MatrixMax(int[][] a){
        int c=a[0].length;
        int b=a.length;
        double maxValue=a[0][0];
        int row = 0,column = 0;
        for(int i=0;i<b;i++){
            for(int j=0;j<c;j++){
                if(maxValue<a[i][j]){
                    maxValue=a[i][j];
                    row=i;
                    column=j;
                }
                
            }
        }System.out.println(maxValue);
                System.out.print(row);
                System.out.print(" ");
                System.out.println(column);
         return a;
    }
   
}
发布了24 篇原创文章 · 获赞 28 · 访问量 2266

猜你喜欢

转载自blog.csdn.net/weixin_46292455/article/details/104972643
今日推荐