javaarraysloopsmaxmin

Finding minimum and maximum in Java 2D array


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 ways that I have tried.

import java.util.*;

class MinMax {
    public static void main(String[] args) {
        int[][] data = {{3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}};
        Arrays.sort(data);
        System.out.println("Minimum = " + data[0]);
        System.out.println("Maximum = " + data[data.length - 1]);
    }
}

This version complies but doesn't run.

import java.util.*;

class MinMax {
    public static void main(String[] args) {
        int[][] data = {{3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}};

    public static int getMaxValue(int[] numbers) {
        int maxValue = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > maxValue) {
                maxValue = numbers[i];
            }
            return maxValue;
            {
                public static int getMinValue (int[] numbers) {
                    int minValue = numbers[0];
                    for (int i = 1; i < numbers.length; i++) {
                        if (numbers[i] < minValue) {
                            minValue = numbers[i];
                        }
                    }
                return minValue;
            }

This version just throws me a bunch of errors in compiling. Any help is greatly appreciated.


Solution

  • Ok, I've kinda fixed your code. Actually your mistake was that you have not been traversing all the cells of your multidimensional array.

    So, I've added additional loop into getMinValue/getMinValue methods and fixed array elements addressing.

    import java.util.*;
    
    class MinMax {
        public static void main(String[] args) {
            int[][] data = {
                    {3, 2, 5},
                    {1, 4, 4, 8, 13},
                    {9, 1, 0, 2},
                    {0, 2, 6, 3, -1, -8}
            };
            System.out.println(getMaxValue(data));
            System.out.println(getMinValue(data));
        }
    
    
        public static int getMaxValue(int[][] numbers) {
            int maxValue = numbers[0][0];
            for (int j = 0; j < numbers.length; j++) {
                for (int i = 0; i < numbers[j].length; i++) {
                    if (numbers[j][i] > maxValue) {
                        maxValue = numbers[j][i];
                    }
                }
            }
            return maxValue;
        }
    
        public static int getMinValue(int[][] numbers) {
            int minValue = numbers[0][0];
            for (int j = 0; j < numbers.length; j++) {
                for (int i = 0; i < numbers[j].length; i++) {
                    if (numbers[j][i] < minValue ) {
                        minValue = numbers[j][i];
                    }
                }
            }
            return minValue ;
        }
    }