Finding minimum and maximum in Java 2D array

前端 未结 4 1280
栀梦
栀梦 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 13:44

    Your problem is: You are sorting the array of int arrays instead of sorting each individual int in each int array.

    To solve this: Loop through each int array in the array of int arrays.

    Instructions for finding the maximum and minimum of a 2D int array using Arrays.sort():

    1. Declare a 2D int array to sort called data.
    2. Declare two ints, one to hold the maximum value, the other the minimum value.
      • The initial value of the maximum should be Integer.MIN_VALUE and the initial value of the minimum should be Integer.MAX_VALUE to make sure that negative values are handled.
    3. Loop through data from 0 to data.length:
      1. Sort data[i]
      2. Check if the first value of data[i] is less than the minimum and change it if it is.
      3. Check if the last value of data[i] is greater than the maximum and change it if it is.
    4. Output your result.

    Example:

    import java.util.Arrays;
    
    public class Main {
        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} };
    
            int maximum = Integer.MIN_VALUE;
            int minimum = Integer.MAX_VALUE;
    
            for(int i = 0; i < data.length; i++) {
                Arrays.sort(data[i]);
    
                if(data[i][0] < minimum) minimum = data[i][0];
                if(data[i][data[i].length - 1] > maximum) maximum = data[i][data[i].length - 1];
            }
    
            System.out.println("Minimum = " + maximum);  
            System.out.println("Maximum = " + minimum); 
        }
    }
    
    0 讨论(0)
  • 2020-12-21 13:46

    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 ;
        }
    }
    
    0 讨论(0)
  • 2020-12-21 13:51

    I have a more fun solution using Java 8 :)

    IntSummaryStatistics stats = Arrays.stream(data).flatMapToInt(Arrays::stream).collect(Collectors.summarizingInt(Integer::intValue));
    int max = stats.getMax();
    int min = stats.getMin();
    

    It's a different solution than yours, obviously. But it does the same thing. To begin with, we convert the 2D array into a Stream of ints. In order to do this first we need to call flatMapToInt. We do this to stream all the elements in the array in a flat way. Imagine if we just start using a single index to iterate over the whole 2D array. It's something like that. Once we have converted the array into a stream, we will use IntSummaryStatistics in order to reuse the same stream for both min and max.

    0 讨论(0)
  • 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);
    }
    

    }

    0 讨论(0)
提交回复
热议问题