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

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

    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];
    }
    

提交回复
热议问题