Finding the max/min value in an array of primitives using Java

前端 未结 15 1810
遥遥无期
遥遥无期 2020-11-22 05:09

It\'s trivial to write a function to determine the min/max value in an array, such as:

/**
 * 
 * @param chars
 * @return the max value in the array of chars         


        
相关标签:
15条回答
  • 2020-11-22 05:23

    You can simply use the new Java 8 Streams but you have to work with int.

    The stream method of the utility class Arrays gives you an IntStream on which you can use the min method. You can also do max, sum, average,...

    The getAsInt method is used to get the value from the OptionalInt

    import java.util.Arrays;
    
    public class Test {
        public static void main(String[] args){
            int[] tab = {12, 1, 21, 8};
            int min = Arrays.stream(tab).min().getAsInt();
            int max = Arrays.stream(tab).max().getAsInt();
            System.out.println("Min = " + min);
            System.out.println("Max = " + max)
        }
    
    }
    

    ==UPDATE==

    If execution time is important and you want to go through the data only once you can use the summaryStatistics() method like this

    import java.util.Arrays;
    import java.util.IntSummaryStatistics;
    
    public class SOTest {
        public static void main(String[] args){
            int[] tab = {12, 1, 21, 8};
            IntSummaryStatistics stat = Arrays.stream(tab).summaryStatistics();
            int min = stat.getMin();
            int max = stat.getMax();
            System.out.println("Min = " + min);
            System.out.println("Max = " + max);
        }
    }
    

    This approach can give better performance than classical loop because the summaryStatistics method is a reduction operation and it allows parallelization.

    0 讨论(0)
  • 2020-11-22 05:24
    import java.util.Random;
    
    public class Main {
    
    public static void main(String[] args) {
       int a[] = new int [100];
       Random rnd = new Random ();
    
        for (int i = 0; i< a.length; i++) {
            a[i] = rnd.nextInt(99-0)+0;
            System.out.println(a[i]);
        }
    
        int max = 0;          
    
        for (int i = 0; i < a.length; i++) {
            a[i] = max;
    
    
            for (int j = i+1; j<a.length; j++) {
                if (a[j] > max) {
                   max = a[j];
                }
    
            }
        }
    
        System.out.println("Max element: " + max);
    }
    }
    
    0 讨论(0)
  • 2020-11-22 05:25
    import java.util.Arrays;
    
    public class apples {
    
      public static void main(String[] args) {
        int a[] = {2,5,3,7,8};
        Arrays.sort(a);
    
         int min =a[0];
        System.out.println(min);
        int max= a[a.length-1];
        System.out.println(max);
    
      }
    
    }
    
    0 讨论(0)
  • 2020-11-22 05:30

    Yes, it's done in the Collections class. Note that you will need to convert your primitive char array to a Character[] manually.

    A short demo:

    import java.util.*;
    
    public class Main {
    
        public static Character[] convert(char[] chars) {
            Character[] copy = new Character[chars.length];
            for(int i = 0; i < copy.length; i++) {
                copy[i] = Character.valueOf(chars[i]);
            }
            return copy;
        }
    
        public static void main(String[] args) {
            char[] a = {'3', '5', '1', '4', '2'};
            Character[] b = convert(a);
            System.out.println(Collections.max(Arrays.asList(b)));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:30

    Example with float:

    public static float getMaxFloat(float[] data) {
    
        float[] copy = Arrays.copyOf(data, data.length);
        Arrays.sort(copy);
        return copy[data.length - 1];
    }
    
    public static float getMinFloat(float[] data) {
    
        float[] copy = Arrays.copyOf(data, data.length);
        Arrays.sort(copy);
        return copy[0];
    }
    
    0 讨论(0)
  • 2020-11-22 05:30

    Pass the array to a method that sorts it with Arrays.sort() so it only sorts the array the method is using then sets min to array[0] and max to array[array.length-1].

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