0

I am a beginner in Java and I am trying to figure out why my method is not returning the largest value in the input array. My idea is the when the method is called, the for loops will search through every value of the array. Then it begins setting the first value as the largest value and any value that is greater than that value then becomes the largest value thereafter.

Any help is much appreciated!

public double displayLargest (double[][] l) {
    for (int x=0; x < l.length-1; x++) {
        for (int y=0; y < l[x].length; y++) {
            double w = l[0][0];
            if (w < l[x][y]) {
                x++;
                y++;
                w = l[x][y];
                maxValue = w;
            }
        }
    }
    System.out.println("The largest value in the array is: " + maxValue);
    return maxValue;
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Alex Dometrius
  • 812
  • 7
  • 20

5 Answers5

2

The following method will return the largest value in the 2D input array of double and it will return null if no values are present.

public Double displayLargest(double[][] l){
    Double maxValue = null;

    for (int x=0; x < l.length; x++) {
        for (int y=0; y < l[x].length; y++) {

            if (maxValue == null || maxValue < l[x][y]) {
                maxValue = l[x][y];
            }
        }
    }

    System.out.println("The largest value in the array is: " + maxValue);

    return maxValue;
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

try

    double maxVal = -Double.MAX_VALUE;  // see http://stackoverflow.com/questions/3884793/why-is-double-min-value-in-not-negative
    for(int x =0; x<l.length; x++){
        for(int y=0; y<l[x].length; y++){

            double w = l[x][y];
            maxVal = Math.max (maxVal, w);
        }
    }

    System.out.println("The largest value in the array is: " +maxVal);

See also https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#min(double,%20double)

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0
double max = l[0][0];
//two loops here
if(max < l[x][y])//inside two loops
{
   max = l[x][y]
}

You are always assigning the first value of the array to w, you should declare it before any loops. Otherwise, you are always comparing the value with l[0][0].

Peixiang Zhong
  • 419
  • 4
  • 8
0

I had given simple method to do so.

int[][] array2 = 
   {{1, 2, 3, 4, 5},
    {6, 7, 8, 9, 10},
    {11, 12, 13, 14, 15},
    {16,17,18,19}};
      int result = Arrays.stream(array2)
    .flatMapToInt(h2 -> Arrays.stream(h2))
    .min()
    .getAsInt();
      System.out.println(result);

this is for max

int[][] array2 = 
   {{1, 2, 3, 4, 5},
    {6, 7, 8, 9, 10},
    {11, 12, 13, 14, 15},
    {16,17,18,19}};
      int result = Arrays.stream(array2)
    .flatMapToInt(h2 -> Arrays.stream(h2))
    .max()
    .getAsInt();
      System.out.println(result);
SmashCode
  • 741
  • 1
  • 8
  • 14
0

I would strongly recommend you start with meaningful variable names. Next, I would urge you to prefer the for-each loop over the traditional for loop (especially when you are nesting loops). Finally, I would default to NaN that way you can handle null and empty arrays. Something like,

public static double displayLargest(double[][] arrayOfArray) {
    double maxValue = Double.NaN;
    if (arrayOfArray != null) {
        for (double[] array : arrayOfArray) { // <-- for each array in arrayOfArray
            for (double value : array) {      // <-- for each value in the array
                if (!Double.isNaN(maxValue)) {
                    maxValue = Double.max(maxValue, value);
                } else { // <-- the first value.
                    maxValue = value;
                }
            }
        }
    }
    System.out.println("The largest value in the array is: " + maxValue);
    return maxValue;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249