Finding minimum and maximum in Java 2D array

前端 未结 4 1286
栀梦
栀梦 2020-12-21 13:02

I have been trying to figure this out for a while and need some help. I need to find the min/max values and print them out for a multidimensional array. Here are the two way

4条回答
  •  孤城傲影
    2020-12-21 14:05

    package array;

    public class Max_number {

    // 2 5 7 9
    // 3 6 8 1
    
    public static void main (String []arg) {
    
        int a[][] = {{2,5,7,9},{3,6,8,1}};
        int max=a[0][0];
        for (int i=0; i<2; i++) //row
        {
            for (int j=0; j<4; j++) //coloum
            {
                if(a[i][j]>max)
                {
                    max=a[i][j];
            }
    
        }
    
    }
        System.out.println("maximum number is"+max);
    }
    

    }

提交回复
热议问题