【问题标题】:Location of the largest and smallest number in an array数组中最大和最小数的位置
【发布时间】:2021-03-27 16:49:16
【问题描述】:

我不知道如何获得数组中最大和最小数字的索引位置。有人可以在这里帮助我吗? 我的代码:

int[] array = {4, 2, 7, 6, -3, -1, -2, 42, 0, -42, 9, -4, 5, -5, -6, -7, -8, -99, 42, 11, 20, 1, 2, 3};

 int smallest = array[0];
 int largest = array[0];

        for (int i = 1; i < array.length; i++){

            if (array[i] > largest) {

                largest = array[i];

            } else if (array[i] < smallest)

                smallest = array[i];
            
        }
        System.out.println("Largest: " + largest);
        System.out.println("Smallest: " + smallest);

我已经有了最大和最小的数字,但是如何找到索引位置。

【问题讨论】:

  • 你觉得i会给你什么?
  • 您不仅需要记住largestsmallest,而且在存储它们的同时还需要存储indexOfLargest = i;indexOfLowest = i;
  • 只需将索引位置写入largestsmallest。然后在打印出结果时,您可以使用保存的索引位置从数组中检索最小值和最大值。

标签: java data-structures hashmap


【解决方案1】:

再创建两个变量来存储最大索引和最小索引,现在每当您在 if-else 语句中为最小和最大分配新值时,也会分配 i 的值。

int smallestInd = 0;
int largestInd = 0;
for (int i = 1; i < array.length; i++){

            if (array[i] > largest) {

                largest = array[i];
                largestInd = i;

            } else if (array[i] < smallest)

                smallest = array[i];
                smallestInd = i;
            
        }

【讨论】:

    【解决方案2】:

    这应该可以工作

        int[] array = {4, 2, 7, 6, -3, -1, -2, 42, 0, -42, 9, -4, 5, -5, -6, -7, -8, -99, 42, 11, 20, 1, 2, 3};
    
        int si, smallest = array[si = 0];
        int li, largest = array[li = 0];
    
        for (int i = 1; i < array.length; i++) {
            if (array[i] > largest) {
                largest = array[li = i];
            } else if (array[i] < smallest) {
                smallest = array[si = i];
            }
        }
        System.out.println("Largest: " + li);
        System.out.println("Smallest: " + si);
    

    或更容易阅读:

        int[] array = {4, 2, 7, 6, -3, -1, -2, 42, 0, -42, 9, -4, 5, -5, -6, -7, -8, -99, 42, 11, 20, 1, 2, 3};
    
        int si = 0, smallest = array[0];
        int li = 0, largest = array[0];
    
        for (int i = 1; i < array.length; i++) {
            if (array[i] > largest) {
                largest = array[i];
                li = i;
            } else if (array[i] < smallest) {
                smallest = array[i];
                si = i;
            }
        }
        System.out.println("Largest: " + li);
        System.out.println("Smallest: " + si);
    

    【讨论】:

      【解决方案3】:
          I was able to came up with two answers : 
          ary.sort();
          console.log(
        `This is max ${ary[0]} of the numbers and here is ${
          ary[-1]
        } the min of the numbers list!`
      );
      _________________________________________________________________
      `   let ary = [8, 13, 2, 5, 44, 1, 5, 55];
          let max = ary[0];
          let min = ary[0];
          let locationMax, locationMin;
          for (i = 0; i <= ary.length; i++) {
            if (ary[i] > max) {
              max = ary[i];
              locationMax = i;
            } else if (ary[i] < min) {
              min = ary[i];
              locationMin = i;
            }
          }
          console.log(
            `This is the location of the max ${locationMax} so The second location number ${locationMin} is for min!`
          );
      

      【讨论】:

      • 那不是 Java。
      猜你喜欢
      • 2018-08-27
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      相关资源
      最近更新 更多