To get the column and row numbers of an element in a two-dimensional array in Java, you can use a combination of loops and indexing.
Here's an example of how to get the column and row numbers of an element in a two-dimensional array in Java:
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { if (array[i][j] == 5) { System.out.println("The element 5 is at row: " + i + ", column: " + j); } } }
In the above example, the array
is a two-dimensional array with 3 rows and 3 columns. The outer for
loop iterates over the rows of the array, and the inner for
loop iterates over the columns of each row.
The if
statement is used to check if the element at the current row and column is equal to 5. If it is, the row and column numbers are printed to the console using the println()
method.
For example, if the element 5 is at row 1 and column 1, the output will be "The element 5 is at row: 1, column: 1".
For more information on working with arrays in Java, you can refer to the documentation for the Arrays
class in the Java API.