Java 在二维数组中查找最小值和最大值。找到的最小值不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27286358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:33:47  来源:igfitidea点击:

Java find min and max value in 2d array. Minimum value found is not correct

javamultidimensional-arraymin

提问by Valravn

Here is my code in which I declare a 2d array with all the values and I'm trying get it to print the min and max values. The max value turns out right but the min value does not. When I run it, the max is 9.73 (which is correct) and the min printed is 5.44, which is incorrect because if you look at the values in the array, you see that 5.29 is the minimum value. Does anyone know why it won't find the correct min value? Thanks in advance!

这是我的代码,我在其中声明了一个包含所有值的二维数组,我正在尝试让它打印最小值和最大值。最大值结果正确,但最小值不正确。当我运行它时,最大值是 9.73(这是正确的),打印的最小值是 5.44,这是不正确的,因为如果您查看数组中的值,您会看到 5.29 是最小值。有谁知道为什么它找不到正确的最小值?提前致谢!

public class Test {
  public static double[][] array1 = //array to be used
   {{7.51, 9.57, 6.28, 5.29, 8.7},
    {8.07, 6.54, 5.44, 8.78, 8.66},
    {9.34, 9.73, 7.19, 6.87, 6.48}};
  public static void main(String[] args) {
    double h = array1[0][0];// highest value
    double l = array1[0][0];// lowest value 
    for (int row = 1; row < array1.length; row++){ //find highest value
      for (int col = 1; col < array1.length; col++){
        if (array1[row][col] > h){
         h = array1[row][col]; 
        }
      }
    }
    for (int r = 1; r < array1.length; r++){ //find lowest value
      for (int c = 1; c < array1.length; c++){
        if (array1[r][c] < l){
         l = array1[r][c]; 
        }
      }
    }
    System.out.println(h);//print highest
    System.out.println(l);//print lowest
  }
}

回答by gprathour

I would say, there are two thingsthat you need to know here.

我想说,这里有两件事你需要知道。

  1. As all are saying the index starts from 0. So in both your loops, it should start from 0 only. So,
  1. 正如所有人都说的索引从 0 开始。所以在你的两个循环中,它应该只从 0 开始。所以,

for (int row = 0; row < array1.length; row++){

for (int row = 0; row < array1.length; row++){

for (int col = 0; col < array1.length; col++){

for (int col = 0; col < array1.length; col++){

for (int r = 0; r < array1.length; r++){

for (int r = 0; r < array1.length; r++){

for (int c = 0; c < array1.length; c++){

for (int c = 0; c < array1.length; c++){

  1. The more important point, which is causing your problem. You are using nested for loops to access a 2D array. What you are writing is,
  1. 更重要的一点,这是导致您的问题。您正在使用嵌套的 for 循环来访问二维数组。你写的是,

for (int r = 1; r < array1.length; r++){

for (int r = 1; r < array1.length; r++){

for (int c = 1; c < array1.length; c++){

for (int c = 1; c < array1.length; c++){

You very well know the outer for loop is for rows and inner for loop is for columns.

您非常清楚外部 for 循环用于行而内部 for 循环用于列。

Now have a look at the values you are using for your conditions array1.lengthyou are using the same value for both the loops. If you try to print this thing, array1.lengththen you will get 3as it is the size of your main array array1. This thing is okay for the outer for loop.

现在看看您为您的条件array1.length使用的值,您对两个循环使用相同的值。如果你尝试打印这个东西,array1.length那么你会得到3你的主数组的大小array1。这对于外部 for 循环来说是可以的。

But the inner loop needs to work as many times as there are elements in each sub array (a 2D array is nothing but an array of arrays) Therefore you need to correct the inner for loop's condition to be,

但是内部循环需要与每个子数组中的元素一样多地工作(二维数组只不过是一个数组数组)因此您需要更正内部 for 循环的条件,

for (int col = 0; col < array1[row].length; col++){

for (int col = 0; col < array1[row].length; col++){

and

for (int c = 0; c < array1[r].length; c++){

for (int c = 0; c < array1[r].length; c++){

Right now even your inner loop is working for two iterations i.e. 1,2 as array1.length returns 3 and your condition c < array1.length;becomes c < 3, so it will only consider the values 9.57, 6.28,and skip 5.29and rest of the elements. When you will update this condition then it will consider all the values.

现在,即使您的内部循环也在进行两次迭代,即 1,2,因为 array1.length 返回 3 并且您的条件c < array1.length;变为c < 3,因此它只会考虑值9.57, 6.28,并跳过5.29和其余元素。当您更新此条件时,它将考虑所有值。

回答by u290629

If this is not a homework and Java8 is allowed to used, I recommend one line code:

如果这不是作业并且允许使用Java8,我推荐一行代码:

double result = Arrays.stream(array1)
    .flatMapToDouble(a -> Arrays.stream(a))
    .min()/max()
    .getAsDouble();

回答by Anjana Daluwatta

You skiped the zero index in the `array

您跳过了 `array 中的零索引

class array1{
 public static void main(String args[]){
     double[][] array1 = {{7.51, 9.57, 6.28, 5.29, 8.7},
                          {8.07, 6.54, 5.44, 8.78, 8.66},
                          {9.34, 9.73, 7.19, 6.87, 6.48}};


      double l = array1[0][0];// lowest value 

for (int r = 0; r < array1.length; r++){ //find lowest value
  for (int c = 0; c < array1.length; c++){
   if(r!=0 || c!=0){
     if (array1[r][c] < l){
       l = array1[r][c]; 
     }
   }
   }
  }
System.out.println(l);//print lowest
  }
}

回答by Dhaval

You Skipped 0 index

你跳过了 0 个索引

for (int r = 0; r < array1.length; r++){ //find lowest value
  for (int c = 0; c < array1.length; c++){

回答by Alex K

It's because you're starting your forloop with r and c equal to 1.

这是因为您以 r 和 c 等于 1 开始您的 forloop。

for (int r = 1; r < array1.length; r++){ //find lowest value
  for (int c = 1; c < array1.length; c++){

You're skipping the entire first array. You need to start them at 0 - then everything will work:

您正在跳过整个第一个数组。您需要从 0 开始它们 - 然后一切都会正常进行:

for (int r = 0; r < array1.length; r++){ //find lowest value
  for (int c = 0; c < array1.length; c++){

回答by Jon Kiparsky

for (int r = 1; r < array1.length; r++){

Java arrays are zero-indexed, so this is ignoring the first array completely. Count from i = 0 instead of from 1

Java 数组是零索引的,因此这完全忽略了第一个数组。从 i = 0 而不是从 1 开始计数

(for i, read "whatever index variable you like" - i is just what everyone uses to count over arrays... :) )

(对于我来说,阅读“你喜欢的任何索引变量”——我就是每个人用来计算数组的东西......:))