Integer.MAX_VALUE和Integer.MIN_VALUE的解释,用于查找数组中的最小值和最大值 [英] Explanation on Integer.MAX_VALUE and Integer.MIN_VALUE to find min and max value in an array

查看:217
本文介绍了Integer.MAX_VALUE和Integer.MIN_VALUE的解释,用于查找数组中的最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

    最小开始设置为 Integer.MAX_VALUE 最大设置为 Integer.MIN_VALUE ,他们以后不必担心最小的特殊情况, 最大还没有值。如果我正在查看的数据有 10 作为第一个值,那么 numbers [i]< smallest 将是真的(因为 10 < Integer.MAX_VALUE )我们将最小更新为 10 。同样, numbers [i]>最大 true 因为 10 > 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.

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

    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 在评论中提出:


    ...如果数组中的第一项大于其余项,则由于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.

    这是真的;这是一个更简单的示例来说明问题(实时副本):

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

    修复它,或者:


    1. 不要使用 else ,或

    最小开头,最大等于第一个元素,然后循环其余元素,保持 else,如果

    Start with smallest and largest equal to the first element, and then loop the remaining elements, keeping the else if.

    以下是第二个示例(实时副本):

    Here's an example of that second one (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
        }
    }
    

    这篇关于Integer.MAX_VALUE和Integer.MIN_VALUE的解释,用于查找数组中的最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆