解释 Integer.MAX_VALUE 和 Integer.MIN_VALUE 在数组中查找最小值和最大值
我似乎不明白 Integer.MAX_VALUE
和 Integer.MIN_VALUE
如何帮助查找数组中的最小值和最大值.
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.
我了解这种方法(下面的伪代码)在查找最小值和最大值时的工作原理:
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]
但是关于这个方法,我不明白Integer.MAX_VALUE
和Integer.MIN_VALUE
的用途:
But as for this method, I don't understand the purpose of Integer.MAX_VALUE
and 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) 给出 2147483647
- System.out.println(Integer.MIN_VALUE) 给出 -2147483648
那么 Integer.MIN_VALUE 和 Integer.MIN_VALUE 在比较中的作用是什么?
So what purpose do Integer.MIN_VALUE and Integer.MIN_VALUE serve in the comparisons?
推荐答案
但是关于这个方法,我不明白Integer.MAX_VALUE和Integer.MIN_VALUE的用途.
but as for this method, I don't understand the purpose of Integer.MAX_VALUE and Integer.MIN_VALUE.
通过将 smallest
设置为 Integer.MAX_VALUE
并将 largest
设置为 Integer.MIN_VALUE
开始,它们以后不必担心 smallest
和 largest
还没有值的特殊情况.如果我正在查看的数据有一个 10
作为第一个值,那么 numbers[i]
10
是 <
Integer.MAX_VALUE
),我们将把 smallest
更新为 10
.同样,numbers[i]>largest
将是 true
,因为 10
是 >
Integer.MIN_VALUE
,我们将更新 largest
.以此类推.
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.
当然,在执行此操作时,您必须确保您正在查看的数据中至少有一个值.否则,您最终会得到 smallest
和 largest
中的杜撰数字.
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
.
注意 Onome Sotu 在评论中提出的观点:
Note the point Onome Sotu makes in the comments:
...如果数组中的第一项大于其余项,则由于 else-if 语句,最大的项将始终为 Integer.MIN_VALUE.
...if the first item in the array is larger than the rest, then the largest item will always be Integer.MIN_VALUE because of the else-if statement.
这是真的;这是一个演示问题的更简单示例(live copy):
Which is true; here's a simpler example demonstrating the problem (live copy):
public class Example
{
public static void main(String[] args) throws Exception {
int[] values = {5, 1, 2};
int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;
for (int value : values) {
if (value < smallest) {
smallest = value;
} else if (value > largest) {
largest = value;
}
}
System.out.println(smallest + ", " + largest); // 1, 2 -- WRONG
}
}
要修复它,要么:
不要使用
else
,或者
从 smallest
和 largest
开始等于第一个元素,然后循环其余元素,保留 else if
.
Start with smallest
and largest
equal to the first element, and then loop the remaining elements, keeping the else if
.
这是第二个示例(live copy):
public class Example
{
public static void main(String[] args) throws Exception {
int[] values = {5, 1, 2};
int smallest = values[0];
int largest = values[0];
for (int n = 1; n < values.length; ++n) {
int value = values[n];
if (value < smallest) {
smallest = value;
} else if (value > largest) {
largest = value;
}
}
System.out.println(smallest + ", " + largest); // 1, 5
}
}
相关文章