编写一个函数来确定数组中的最小/最大值是很简单的,例如:

/**
 * 
 * @param chars
 * @return the max value in the array of chars
 */
private static int maxValue(char[] chars) {
    int max = chars[0];
    for (int ktr = 0; ktr < chars.length; ktr++) {
        if (chars[ktr] > max) {
            max = chars[ktr];
        }
    }
    return max;
}

但这不是已经在某个地方做过了吗?


当前回答

使用reduce()的解决方案:

int[] array = {23, 3, 56, 97, 42};
// directly print out
Arrays.stream(array).reduce((x, y) -> x > y ? x : y).ifPresent(System.out::println);

// get the result as an int
int res = Arrays.stream(array).reduce((x, y) -> x > y ? x : y).getAsInt();
System.out.println(res);
>>
97
97

在上面的代码中,reduce()以可选格式返回数据,您可以通过getAsInt()将其转换为int。

如果我们想将最大值与某个数字进行比较,我们可以在reduce()中设置一个起始值:

int[] array = {23, 3, 56, 97, 42};
// e.g., compare with 100
int max = Arrays.stream(array).reduce(100, (x, y) -> x > y ? x : y);
System.out.println(max);
>>
100

在上面的代码中,当reduce()以标识(起始值)作为第一个参数时,它将返回与标识相同格式的数据。有了这个属性,我们可以将这个解决方案应用到其他数组:

double[] array = {23.1, 3, 56.6, 97, 42};
double max = Arrays.stream(array).reduce(array[0], (x, y) -> x > y ? x : y);
System.out.println(max);
>>
97.0

其他回答

你可以使用IntStream和max()方法轻松完成。

例子

public static int maxValue(final int[] intArray) {
  return IntStream.range(0, intArray.length).map(i -> intArray[i]).max().getAsInt();
}

解释

range(0, intArray.length) -获取包含intArray中所有元素的流。 map(i -> intArray[i]) -将流中的每个元素映射到intArray的一个实际元素。 max() -获取此流的最大元素为OptionalInt。 getAsInt() -打开OptionalInt。(你也可以在这里使用:orElse(0),以防OptionalInt为空。)

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

谷歌Guava库在其char, int, long等类中有min和max方法。

所以你可以简单地使用:

Chars.min(myarray)

不需要转换,并且可以有效地实现。

使用reduce()的解决方案:

int[] array = {23, 3, 56, 97, 42};
// directly print out
Arrays.stream(array).reduce((x, y) -> x > y ? x : y).ifPresent(System.out::println);

// get the result as an int
int res = Arrays.stream(array).reduce((x, y) -> x > y ? x : y).getAsInt();
System.out.println(res);
>>
97
97

在上面的代码中,reduce()以可选格式返回数据,您可以通过getAsInt()将其转换为int。

如果我们想将最大值与某个数字进行比较,我们可以在reduce()中设置一个起始值:

int[] array = {23, 3, 56, 97, 42};
// e.g., compare with 100
int max = Arrays.stream(array).reduce(100, (x, y) -> x > y ? x : y);
System.out.println(max);
>>
100

在上面的代码中,当reduce()以标识(起始值)作为第一个参数时,它将返回与标识相同格式的数据。有了这个属性,我们可以将这个解决方案应用到其他数组:

double[] array = {23.1, 3, 56.6, 97, 42};
double max = Arrays.stream(array).reduce(array[0], (x, y) -> x > y ? x : y);
System.out.println(max);
>>
97.0

获取数组的最小/最大值的基本方法。如果您需要无序数组,您可以创建一个副本或将其传递给返回最小值或最大值的方法。如果不是,排序数组更好,因为它在某些情况下执行得更快。

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