I wrote a code that should find the difference between the max and min elements of the array. The code runs without error, but there is no result itself, that is, the number is not displayed. Help to understand the error.

public class Lab4_1 { public static void main(String [] args) { int[] array = {2, 5, 7, 8, 3, 0}; } public static int range(int [] array, int index, int min, int max) { if (index == array.length) { if (index == 0) return 0; else return max - min; } else { int value = array[index]; return range(array, index + 1, Math.min(value, min), Math.max(value, max)); } } public static int range(int [] array){ return range(array, 0, Integer.MAX_VALUE, Integer.MIN_VALUE); } } 

    2 answers 2

    If you run the code, nothing really happens. You have nowhere lines

     System.out.println(); 

    In general, I do not really understand why you need two overridden methods.

    My solution to the whole problem immediately in main

      int[] array = {2, 5, 7, 8, 3, 0}; Arrays.sort(array); System.out.println(array[array.length - 1] - array[0]); 

      Your code is still wrong, even if you force it to print. To find the difference between the minimum and maximum, use this method:

       public static OptionalInt range(int... array) { OptionalInt range = OptionalInt.empty(); if (array != null && array.length > 0) { int min = array[0]; int max = min; for (int n : array) { if (n < min) { min = n; } else if (n > max) { max = n; } } range = OptionalInt.of(max - min); } return range; } 

      You can call it in two ways:

       int[] arr = {2, 4, 6, 8}; OptionalInt range = range(arr); 

      or

       OptionalInt range = range(1, 2, 3, 5); 

      The method returns an OptionalInt , not a number, because there is not always a minimum and a maximum. They are not present when the array is empty or when there is no array itself and the method received null .

      To print the OptionalInt you can use the ifPresent() method, or, starting with Java 9, the ifPresentOrElse() method. For example:

       int[] arr = {2, 4, 6, 8}; range(arr).ifPresentOrElse(System.out::println, () -> System.out.println("no range")); arr = null; range(arr).ifPresentOrElse(System.out::println, () -> System.out.println("no range")); arr = new int[0]; range(arr).ifPresentOrElse(System.out::println, () -> System.out.println("no range")); range(1, 2, 3, 5).ifPresent(System.out::println); range().ifPresent(System.out::println); // ничего не напечатает range(null).ifPresent(System.out::println); // ничего не напечатает 

      Using sorting, to find the minimum and maximum, is less efficient.

      • one
        Rostislav, why is everything so difficult? - michael_best
      • What exactly is difficult, OptionalInt and work with it? Instead of returning OptionalInt.empty() you can throw an exception, but then calling this method will be even more difficult. In any case, it is necessary to take into account situations where there is simply no minimum and maximum, and therefore there is no difference between them. In this case, you can not return any number. - Rostislav Red