j.doe - 1 month ago 4x

Java Question

I am working on a method that takes a 2d array and find the min value in each row. After it find the min in the row, I want that to go into another array. I have the array made, but I keep getting nulls in the array. This is probably an easy fix, but I've been looking at this for so long, I need some help.

`public static double[][] largeArray;`

public static double[] minArray;

public static double min;

private static void min()

{

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

{

min = largeArray[row][0];

for(int col = 1; col > largeArray[row].length; col++)

{

if ( largeArray[row][col] < min )

min = largeArray[row][col];

for(int t=0; t<minArray.length; t++)

{

min = minArray[row];

}

}

}

}

Thank you for looking at this!

Answer

You aren't storing the `min`

value(s) in `minArray`

. You can also use `Math.min(double, double)`

in your loop. Finally, you want `<`

(not `>`

) in your inner loop condition and you could use `Arrays.toString(double[])`

to display the `minArray`

when you have finished *initializing* it. Something like,

```
private static void min() {
minArray = new double[largeArray.length]; // <-- initialize the array
for (int row = 0; row < largeArray.length; row++) {
min = largeArray[row][0];
for (int col = 1; col < largeArray[row].length; col++) {
min = Math.min(min, largeArray[row][col]);
}
minArray[row] = min;
}
System.out.println(Arrays.toString(minArray));
}
```

Source (Stackoverflow)

Comments