【问题标题】:10x10 Two dimensional array and search minimum, maximum value and its index location10x10 二维数组及搜索最小值、最大值及其索引位置
【发布时间】:2016-02-24 16:21:51
【问题描述】:
int[][] numList = new int[10][10];
        int column;
        for (int row = 0; row < 10; row++) {
            for (column = 0; column < 10; column++) {
                numList[row][column] = (int) (Math.random() * 100);
                System.out.print(numList[row][column] + "\t");
            }
            System.out.println("");
        }

【问题讨论】:

  • 欢迎来到 SO。请说明您的问题。
  • 我想知道基于 10x10 矩阵的最大值和最小值。
  • 请编辑您的问题:您要做什么,代码 sn-p 应该是什么,等等...

标签: java arrays for-loop


【解决方案1】:
public class Test1 {
public static void main(String[] args) {
    int numList[][]=new int[10][10]; 
    int max=0;
    int min=999;
    int minX=0;
    int maxX=0;
    int minY=0;
    int maxY=0;
    for (int row = 0; row < 10; row++) {
        for (int column = 0; column < 10; column++) {
                int num=(int)(Math.random()*100);
                numList[row][column] = num;
                if(num>max){
                    max=num;
                    maxX=row;
                    maxY=column;
                }
                if(num<min){
                    min=num;
                    minX=row;
                    minY=column;
                }
                System.out.print(numList[row][column] + "\t");
         }
                System.out.println("");
    }
    System.out.println("Max value=>"+max+"["+maxX+","+maxY+"]");
    System.out.println("Min value=>"+min+"["+minX+","+minY+"]");
}

}

【讨论】:

    【解决方案2】:

    您的代码只是将数据插入二维数组。要查找数组中的最小值和最大值及其对应的索引,您需要编写一个函数search,如下所示:

    public class prog{
    
        public void search(  int[][] numList )
     {
         int max=0;
         int maxX=0;
         int maxY=0;
         int min=0;
         int minX=0;
         int minY=0;
         for (int row = 0; row < 10; row++) {
                for (int column = 0; column < 10; column++) {
                    if( min >  numList[row][column])
                    {
                         min = numList[row][column];
                         minX=row;
                         minY= column;
                    }
                    if( max <  numList[row][column])
                    {
                        max= numList[row][column];
                        maxX=row;
                        maxY=column;
                    }
                }
         }
          System.out.print("MinX:"+ minX+ " MinY: "+ minY+ " MaxX:"+ maxX+ " MaxY:"+ maxY);
     }
    public static void main(String[] args){
    
            int[][] numList = new int[10][10];
            int column;
            for (int row = 0; row < 10; row++) {
                for (column = 0; column < 10; column++) {
                    numList[row][column] = (int) (Math.random() * 100);
                    System.out.print(numList[row][column] + "\t");
                }
                System.out.println("");
            }
            prog obj = new prog();
            obj.search( numList);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-06
      • 1970-01-01
      • 2015-10-24
      • 2021-03-22
      • 2021-07-09
      • 2018-05-22
      相关资源
      最近更新 更多