简体   繁体   中英

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

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!

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

I would say, there are two things that 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,

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

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

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

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,

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

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.

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

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 (int col = 0; col < array1[row].length; col++){

and

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

Right now even your inner loop is working for two iterations ie 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.29 and rest of the elements. When you will update this condition then it will consider all the values.

You Skipped 0 index

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

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

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

You skiped the zero index in the `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
  }
}

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

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:

for (int r = 0; r < array1.length; r++){ //find lowest value
  for (int c = 0; c < array1.length; c++){
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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM