How to get min/Max value of a all column in a two-dimensional arra

so for example, lets say you have the following 2d array:

1 2 3
4 5 6
7 8 9

Than the out put should be:

-- col 1 --
min value at col 1 is 1
max value at col 1 is 7
-- col 2 --
min value at col 2 is 2
max value at col 2 is 8
-- col 3 --
min value at col 3 is 3
max value at col 3 is 9

public static void col(int[][] array) {

        for (int i = 0; i < array.length; i++) {
            int minValue =  array[i][0];
            int maxValue = array[i][0];
            for (int j = 0; j < array[i].length; j++) {
                if (minValue > array[j][i]) {
                    minValue = array[j][i];
                }
                if (maxValue < array[j][i]) {
                    maxValue = array[j][i];
                }
            }
        }

    }

getting an error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

Dani AI

Generated

Brief diagnosis — the loop indices are mixed (rows vs columns). was right to call out the index confusion: when scanning a column you must iterate rows with the row count, and when scanning rows you must use the column count. The posted code uses one loop bound for columns but then indexes as if it were the row bound, which causes an out-of-range access on non-square or ragged arrays.

Correct approach (algorithm):

  • Decide names: use r for row and c for column so the intent is clear.
  • Compute the number of columns safely (check for empty or null rows).
  • For each column c, scan every row r that actually has that column and track min/max.
  • Initialize min/max from the first valid cell in that column (safer than hardcoding Integer limits).

Example implementation:

public static void colMinMax(int[][] a) {
    if (a == null || a.length == 0) return;
    int cols = 0;
    for (int r = 0; r < a.length; r++) if (a[r] != null) cols = Math.max(cols, a[r].length);

    for (int c = 0; c < cols; c++) {
        boolean found = false;
        int min = 0, max = 0;
        for (int r = 0; r < a.length; r++) {
            if (a[r] == null || c >= a[r].length) continue;
            int v = a[r][c];
            if (!found) { min = max = v; found = true; }
            else {
                if (v < min) min = v;
                if (v > max) max = v;
            }
        }
        if (found) System.out.printf("col %d: min=%d max=%d%n", c + 1, min, max);
    }
}

Troubleshooting notes:

  • If the input is guaranteed rectangular, cols = a[0].length is fine after an empty-array check.
  • For ragged arrays, the above code skips missing cells.
  • Add defensive checks for null rows and empty arrays to avoid NPE/AIOOBE.
  • Use clear variable names (r, c) and keep row/column bounds distinct to prevent mixing them.

You didn't say what your question was, but...
decide whether you are using i,j or row,col - don't mix them
Don't forget to print the answers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.