there are 2 dimension arrays, how can print out the position of value than 4?

for example

0 2 3 2 2 2
0 1 1 2 5 3
1 2 3 3 2 1

print out:
The pos is ( 4,1).

Dani AI

Generated

A direct, practical approach (building on 's suggestion): iterate the 2D array with index-aware loops, test each element against the threshold, and print or collect the matching coordinates. The key clarifications often missing in short answers are the coordinate convention (row-first vs column-first), zero-based indexing in Java, and handling ragged rows.

int threshold = 4;
int[][] a = /* existing data */;

for (int row = 0; row < a.length; row++) {
    for (int col = 0; col < a[row].length; col++) {
        if (a[row][col] > threshold) {
            // Common convention: (row, col)
            System.out.println("Found at (row=" + row + ", col=" + col + "): " + a[row][col]);
            // If the posting expects (x,y) where x=column, y=row, print (col, row) instead.
        }
    }
}

Notes and pitfalls:

  • Java arrays are zero-based; decide and document whether output uses zero-based or one-based coordinates.
  • Use a[row].length because rows can be ragged (different lengths).
  • To stop at the first match, return from the method or use a labelled break for the outer loop.
  • Enhanced for loops hide indices; classic for or IntStream.range is clearer when indexes are needed.
  • Time is O(rows * cols); for very large arrays consider early exit or streaming strategies.

If the thread was cross-posted (as observed), consolidation prevents duplicated effort. For general reference on Java arrays and indexing, see the Java tutorial on arrays: Java arrays tutorial.

Recommended Answers

All 2 Replies

You could construct two for loops which search through the array - one inside the other. Then when you find which value you are looking at - just print out the number each bit of the for loop got to.

didn't you post this on javaboutique as well? i posted an answer on there.

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.