• <legend id='R1Vqo'><style id='R1Vqo'><dir id='R1Vqo'><q id='R1Vqo'></q></dir></style></legend>
    1. <i id='R1Vqo'><tr id='R1Vqo'><dt id='R1Vqo'><q id='R1Vqo'><span id='R1Vqo'><b id='R1Vqo'><form id='R1Vqo'><ins id='R1Vqo'></ins><ul id='R1Vqo'></ul><sub id='R1Vqo'></sub></form><legend id='R1Vqo'></legend><bdo id='R1Vqo'><pre id='R1Vqo'><center id='R1Vqo'></center></pre></bdo></b><th id='R1Vqo'></th></span></q></dt></tr></i><div id='R1Vqo'><tfoot id='R1Vqo'></tfoot><dl id='R1Vqo'><fieldset id='R1Vqo'></fieldset></dl></div>

        <small id='R1Vqo'></small><noframes id='R1Vqo'>

      1. <tfoot id='R1Vqo'></tfoot>

          <bdo id='R1Vqo'></bdo><ul id='R1Vqo'></ul>

        解释 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_VALUE 和 Integer.MIN_VALUE 在数组中查找最小值和最大值)

        <small id='v40YK'></small><noframes id='v40YK'>

            <i id='v40YK'><tr id='v40YK'><dt id='v40YK'><q id='v40YK'><span id='v40YK'><b id='v40YK'><form id='v40YK'><ins id='v40YK'></ins><ul id='v40YK'></ul><sub id='v40YK'></sub></form><legend id='v40YK'></legend><bdo id='v40YK'><pre id='v40YK'><center id='v40YK'></center></pre></bdo></b><th id='v40YK'></th></span></q></dt></tr></i><div id='v40YK'><tfoot id='v40YK'></tfoot><dl id='v40YK'><fieldset id='v40YK'></fieldset></dl></div>

              <tfoot id='v40YK'></tfoot>
              <legend id='v40YK'><style id='v40YK'><dir id='v40YK'><q id='v40YK'></q></dir></style></legend>

                  <tbody id='v40YK'></tbody>
                  <bdo id='v40YK'></bdo><ul id='v40YK'></ul>
                  本文介绍了解释 Integer.MAX_VALUE 和 Integer.MIN_VALUE 在数组中查找最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我似乎不明白 Integer.MAX_VALUEInteger.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_VALUEInteger.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 开始,它们以后不必担心 smallestlargest 还没有值的特殊情况.如果我正在查看的数据有一个 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.

                    当然,在执行此操作时,您必须确保您正在查看的数据中至少有一个值.否则,您最终会得到 smallestlargest 中的杜撰数字.

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

                    要修复它,要么:

                    1. 不要使用else,或者

                    smallestlargest 开始等于第一个元素,然后循环其余元素,保留 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
                        }
                    }
                    

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

                    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                    <legend id='pFgEy'><style id='pFgEy'><dir id='pFgEy'><q id='pFgEy'></q></dir></style></legend>

                    <i id='pFgEy'><tr id='pFgEy'><dt id='pFgEy'><q id='pFgEy'><span id='pFgEy'><b id='pFgEy'><form id='pFgEy'><ins id='pFgEy'></ins><ul id='pFgEy'></ul><sub id='pFgEy'></sub></form><legend id='pFgEy'></legend><bdo id='pFgEy'><pre id='pFgEy'><center id='pFgEy'></center></pre></bdo></b><th id='pFgEy'></th></span></q></dt></tr></i><div id='pFgEy'><tfoot id='pFgEy'></tfoot><dl id='pFgEy'><fieldset id='pFgEy'></fieldset></dl></div>
                      <bdo id='pFgEy'></bdo><ul id='pFgEy'></ul>
                    • <tfoot id='pFgEy'></tfoot>

                              <tbody id='pFgEy'></tbody>

                            <small id='pFgEy'></small><noframes id='pFgEy'>