Integer.MAX_VALUE和Integer.MIN_VALUE的解释,用于查找数组中的最小值和最大值

时间:2022-09-26 18:49:17

I don't seem to understand how Integer.MAX_VALUE and Integer.MIN_VALUE help in finding the min and max value in an array.

我似乎不明白Integer.MAX_VALUE和Integer.MIN_VALUE如何帮助查找数组中的最小值和最大值。

I understand how this method (pseudocode below) works when finding the min and max values:

我理解这个方法(下面的伪代码)在找到最小值和最大值时是如何工作的:

max = A[0], min = A[0]
for each i in A
  if A[i] > max then max = A[i]
  if A[i] < min then min = A[i] 

But as for this method, I don't understand the purpose of Integer.MAX_VALUE and Integer.MIN_VALUE:

但至于这个方法,我不明白Integer.MAX_VALUE和Integer.MIN_VALUE的目的:

import java.util.Scanner;

class MyClass {

    public static void main(String[] args) {

        int[] numbers; // declaring the data type of numbers
        numbers = new int[3]; //assigning the number of values numbers will contain
        int smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE;

        Scanner input = new Scanner(System.in);

        System.out.println("Please enter 3 numbers");

        for(int counter = 0; counter<numbers.length;counter++) {
            numbers[counter] = input.nextInt();
        }

        for(int i = 0; i<numbers.length; i++) {
            if(numbers[i]<smallest)
                smallest = numbers[i];
            else if(numbers[i]>largest)
                largest = numbers[i];
        }

        System.out.println("Largest is "+largest);
        System.out.println("Smallest is "+smallest);
    }

}
  • System.out.println(Integer.MAX_VALUE) gives 2147483647
  • System.out.println(Integer.MAX_VALUE)给出2147483647
  • System.out.println(Integer.MIN_VALUE) gives -2147483648
  • System.out.println(Integer.MIN_VALUE)给出-2147483648

So what purpose do Integer.MIN_VALUE and Integer.MIN_VALUE serve in the comparisons?

那么Integer.MIN_VALUE和Integer.MIN_VALUE在比较中的用途是什么?

3 个解决方案

#1


21  

but as for this method, I don't understand the purpose of Integer.MAX_VALUE and Integer.MIN_VALUE.

但至于这个方法,我不明白Integer.MAX_VALUE和Integer.MIN_VALUE的目的。

By starting out with smallest set to Integer.MAX_VALUE and largest set to Integer.MIN_VALUE, they don't have to worry later about the special case where smallest and largest don't have a value yet. If the data I'm looking through has a 10 as the first value, then numbers[i]<smallest will be true (because 10 is < Integer.MAX_VALUE) and we'll update smallest to be 10. Similarly, numbers[i]>largest will be true because 10 is > Integer.MIN_VALUE and we'll update largest. And so on.

通过将最小值设置为Integer.MAX_VALUE并将最大值设置为Integer.MIN_VALUE,稍后他们不必担心最小值和最大值没有值的特殊情况。如果我正在查看的数据有10作为第一个值,那么数字[i] <最小将为真(因为10是 最大将是真的,因为10> Integer.MIN_VALUE,我们将更新最大。等等。 )并且我们将最小值更新为10.同样,数字[i>

Of course, when doing this, you must ensure that you have at least one value in the data you're looking at. Otherwise, you end up with apocryphal numbers in smallest and largest.

当然,在执行此操作时,您必须确保在您正在查看的数据中至少有一个值。否则,你最终会得到最小和最大的伪造数字。

#2


10  

Instead of initializing the variables with arbitrary values (for example int smallest = 9999, largest = 0) it is safer to initialize the variables with the largest and smallest values representable by that number type (that is int smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE).

不是用任意值初始化变量(例如int int = 9999,maximum = 0),而是初始化具有该数字类型可表示的最大值和最小值的变量更安全(即int int = Integer.MAX_VALUE,maximum = Integer.MIN_VALUE的)。

Since your integer array cannot contain a value larger than Integer.MAX_VALUE and smaller than Integer.MIN_VALUE your code works across all edge cases.

由于整数数组不能包含大于Integer.MAX_VALUE且小于Integer.MIN_VALUE的值,因此您的代码适用于所有边缘情况。

#3


3  

By initializing the min/max values to their extreme opposite, you avoid any edge cases of values in the input: Either one of min/max is in fact one of those values (in the case where the input consists of only one of those values), or the correct min/max will be found.

通过将min / max值初始化为极端相反的值,可以避免输入中任何值的边缘情况:min / max中的任何一个实际上是其中一个值(在输入仅包含其中一个值的情况下) ),或找到正确的最小/最大值。

It should be noted that primitive types must have a value. If you used Objects (ie Integer), you could initialize value to null and handle that special case for the first comparison, but that creates extra (needless) code. However, by using these values, the loop code doesn't need to worry about the edge case of the first comparison.

应该注意,原始类型必须具有值。如果你使用了Objects(即Integer),你可以将value初始化为null并处理第一次比较的特殊情况,但这会产生额外的(不必要的)代码。但是,通过使用这些值,循环代码不需要担心第一次比较的边缘情况。

Another alternative is to set both initial values to the first value of the input array (never a problem - see below) and iterate from the 2nd element onward, since this is the only correct state of min/max after one iteration. You could iterate from the 1st element too - it would make no difference, other than doing one extra (needless) iteration over the first element.

另一种方法是将两个初始值都设置为输入数组的第一个值(从来没有问题 - 见下文),然后从第二个元素向前迭代,因为这是一次迭代后唯一正确的最小值/最大值。你也可以从第一个元素迭代 - 除了在第一个元素上做一个额外的(不必要的)迭代之外,它没有任何区别。

The only sane way of dealing with inout of size zero is simple: throw an IllegalArgumentException, because min/max is undefined in this case.

处理大小为零的唯一理智方法很简单:抛出IllegalArgumentException,因为在这种情况下min / max是未定义的。

#1


21  

but as for this method, I don't understand the purpose of Integer.MAX_VALUE and Integer.MIN_VALUE.

但至于这个方法,我不明白Integer.MAX_VALUE和Integer.MIN_VALUE的目的。

By starting out with smallest set to Integer.MAX_VALUE and largest set to Integer.MIN_VALUE, they don't have to worry later about the special case where smallest and largest don't have a value yet. If the data I'm looking through has a 10 as the first value, then numbers[i]<smallest will be true (because 10 is < Integer.MAX_VALUE) and we'll update smallest to be 10. Similarly, numbers[i]>largest will be true because 10 is > Integer.MIN_VALUE and we'll update largest. And so on.

通过将最小值设置为Integer.MAX_VALUE并将最大值设置为Integer.MIN_VALUE,稍后他们不必担心最小值和最大值没有值的特殊情况。如果我正在查看的数据有10作为第一个值,那么数字[i] <最小将为真(因为10是 最大将是真的,因为10> Integer.MIN_VALUE,我们将更新最大。等等。 )并且我们将最小值更新为10.同样,数字[i>

Of course, when doing this, you must ensure that you have at least one value in the data you're looking at. Otherwise, you end up with apocryphal numbers in smallest and largest.

当然,在执行此操作时,您必须确保在您正在查看的数据中至少有一个值。否则,你最终会得到最小和最大的伪造数字。

#2


10  

Instead of initializing the variables with arbitrary values (for example int smallest = 9999, largest = 0) it is safer to initialize the variables with the largest and smallest values representable by that number type (that is int smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE).

不是用任意值初始化变量(例如int int = 9999,maximum = 0),而是初始化具有该数字类型可表示的最大值和最小值的变量更安全(即int int = Integer.MAX_VALUE,maximum = Integer.MIN_VALUE的)。

Since your integer array cannot contain a value larger than Integer.MAX_VALUE and smaller than Integer.MIN_VALUE your code works across all edge cases.

由于整数数组不能包含大于Integer.MAX_VALUE且小于Integer.MIN_VALUE的值,因此您的代码适用于所有边缘情况。

#3


3  

By initializing the min/max values to their extreme opposite, you avoid any edge cases of values in the input: Either one of min/max is in fact one of those values (in the case where the input consists of only one of those values), or the correct min/max will be found.

通过将min / max值初始化为极端相反的值,可以避免输入中任何值的边缘情况:min / max中的任何一个实际上是其中一个值(在输入仅包含其中一个值的情况下) ),或找到正确的最小/最大值。

It should be noted that primitive types must have a value. If you used Objects (ie Integer), you could initialize value to null and handle that special case for the first comparison, but that creates extra (needless) code. However, by using these values, the loop code doesn't need to worry about the edge case of the first comparison.

应该注意,原始类型必须具有值。如果你使用了Objects(即Integer),你可以将value初始化为null并处理第一次比较的特殊情况,但这会产生额外的(不必要的)代码。但是,通过使用这些值,循环代码不需要担心第一次比较的边缘情况。

Another alternative is to set both initial values to the first value of the input array (never a problem - see below) and iterate from the 2nd element onward, since this is the only correct state of min/max after one iteration. You could iterate from the 1st element too - it would make no difference, other than doing one extra (needless) iteration over the first element.

另一种方法是将两个初始值都设置为输入数组的第一个值(从来没有问题 - 见下文),然后从第二个元素向前迭代,因为这是一次迭代后唯一正确的最小值/最大值。你也可以从第一个元素迭代 - 除了在第一个元素上做一个额外的(不必要的)迭代之外,它没有任何区别。

The only sane way of dealing with inout of size zero is simple: throw an IllegalArgumentException, because min/max is undefined in this case.

处理大小为零的唯一理智方法很简单:抛出IllegalArgumentException,因为在这种情况下min / max是未定义的。