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

前端 未结 15 2140
遥遥无期
遥遥无期 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:30

    The basic way to get the min/max value of an Array. If you need the unsorted array, you may create a copy or pass it to a method that returns the min or max. If not, sorted array is better since it performs faster in some cases.

    public class MinMaxValueOfArray {
        public static void main(String[] args) {
            int[] A = {2, 4, 3, 5, 5};
            Arrays.sort(A);
            int min = A[0];
            int max = A[A.length -1];
            System.out.println("Min Value = " + min);        
            System.out.println("Max Value = " + max);
        }
    }
    

提交回复
热议问题