簡體   English   中英

Integer.MAX_VALUE和Integer.MIN_VALUE的解釋,用於查找數組中的最小值和最大值

[英]Explanation on Integer.MAX_VALUE and Integer.MIN_VALUE to find min and max value in an array

我似乎不明白Integer.MAX_VALUEInteger.MIN_VALUE如何幫助查找數組中的最小值和最大值。

我理解這個方法(下面的偽代碼)在找到最小值和最大值時是如何工作的:

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_VALUEInteger.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在比較中的用途是什么?

但至於這個方法,我不明白Integer.MAX_VALUE和Integer.MIN_VALUE的目的。

通過將smallest設置為Integer.MAX_VALUE並將largest設置為Integer.MIN_VALUE ,稍后他們不必擔心smallestlargest沒有值的特殊情況。 如果我正在查看的數據有10作為第一個值,則numbers[i]<smallest將為真(因為10< Integer.MAX_VALUE ),我們將smallest更新為10 同樣, numbers[i]>largest將為true因為10 > Integer.MIN_VALUE ,我們將更新largest 等等。

當然,在執行此操作時,您必須確保在您正在查看的數據中至少有一個值。 否則,你最終會得到smallestlargest偽造數字。


請注意Onome Sotu在評論中提出的觀點:

...如果數組中的第一項大於其余項,則由於else-if語句,最大項將始終為Integer.MIN_VALUE。

這是真的; 這是一個更簡單的例子來演示問題( 實時復制 ):

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 ,或

  2. smallestlargest等於第一個元素開始,然后循環其余元素,保留else if

這是第二個例子( 實時復制 ):

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

不是用任意值初始化變量(例如int smallest = 9999, largest = 0 ),而是初始化具有該數字類型可表示的最大值和最小值的變量更安全(即int smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE )。

由於整數數組不能包含大於Integer.MAX_VALUE且小於Integer.MIN_VALUE您的代碼適用於所有邊緣情況。

通過將min / max值初始化為極端相反的值,可以避免輸入中任何值的邊緣情況:min / max中的任何一個實際上是這些值中的一個(在輸入僅包含其中一個值的情況下) ),或找到正確的最小/最大值。

應該注意,原始類型必須具有值。 如果你使用了Objects(即Integer ),你可以將value初始化為null並處理第一次比較的特殊情況,但這會產生額外的(不必要的)代碼。 但是,通過使用這些值,循環代碼不需要擔心第一次比較的邊緣情況。

另一種方法是將兩個初始值都設置為輸入數組的第一個值(從來沒有問題 - 見下文),然后從第二個元素開始迭代,因為這是一次迭代后唯一正確的最小/最大值狀態。 你也可以從第一個元素迭代 - 除了在第一個元素上做一個額外的(不必要的)迭代之外,它沒有任何區別。

處理大小為零的唯一理智方法很簡單:拋出IllegalArgumentException ,因為在這種情況下min / max是未定義的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM