for (int i = 0 ; i < size; i++)
    for(int j = 0 ; j < size ; j++)
    {
         if ( arr[i][j] == 88)
         {
              `save this 2 indexes`
              break;
         }
    }
Answer from Ofer on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 46676054 › java-finding-an-index-in-a-2d-array › 46676515
Java- finding an index in a 2D array - Stack Overflow
October 10, 2017 - If you really want to use the index of function, you need to switch into List(i.e. ArrayList, Vector etc) of lists .
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-and-return-index-of-given-string-in-a-multidimensional-array
Find and return index of given String in a Multidimensional Array - GeeksforGeeks
September 26, 2023 - let index = flattenedArr.indexOf(keyString); if (index !== -1) { // If the keyString is found, calculate the row and column indices. let rows = stringArr.length; let cols = stringArr[0].length; return new Tuple(Math.floor(index / cols), index ...
🌐
DEV Community
dev.to › realedwintorres › accessing-elements-in-2d-arrays-in-java-1o7i
Accessing Elements in 2D Arrays in Java - DEV Community
November 28, 2021 - When using 2D arrays, use two indexes. The first index is for the row. The second index is for the column. In other words, each element of a 2D array is an array. The first index accesses an array element. Then use a second index to access the elements in that array.
Find elsewhere
Top answer
1 of 3
2

My Java is a bit rusty but I'm not seeing any logical issues with the code doing what you want it to.

Since you return just the row or column index, the caller can't tell from that if the match was a row or a column.

2 of 3
1

There are several issues with this code.

The biggest one is that you use != to check for string equality instead of !String.equals().

The parameter n shouldn't need to be specified, since it can be inferred from the dimensions of myArray. As others have noted, the function is weird in that it ignores the first row and the first column, that it uses 0 as a special indicator that no match was found, and that the return value doesn't tell you whether it was a row or column that was found.

I'm not a fan of variable names i, j, and k. Variable names like row and col would have been clearer. The flag variable isError can be eliminated if you just structure the inner loops properly. (In general, flag variables are a poor way to direct flow of control; keywords like break, continue, and return are preferable.)

There is no sense in putting a test like if (j == n …) inside the loop, since it is only relevant when the loop terminates.

I'd write something more like this:

for (int row = 0; row < myArray.length; row++) {
    int col;
    for (col = myArray[row].length - 1; col >= 0; col--) {
        if (row != col && !"error".equals(myArray[row][col])) {
            break;
        }
    }
    if (col < 0) {
        return row;  // Every relevant column in this row is "error"
    }
}
…
return …;  // Some value to indicate that no match was found
🌐
Stack Overflow
stackoverflow.com › questions › 33281997 › use-indexof-for-2d-array-in-java
Use indexOf for 2D array in java - Stack Overflow
November 7, 2016 - Hello I create a 2D array and i want to find the position from the first 2 in the array. And after to add the index in a new ArrayList. But this code doesn't work. Any idea for the problem? import java.util.ArrayList; class Test { public static void main(String[] args) { ArrayList<Integer> tableau = new ArrayList<Integer>(); int[][] tab = { {1,1,1,1,1,1,2,1,1,2,1,1,1,2,1,2,1,1,1,1,1}, {1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1}, }; for (int i = 0; i < tab.length; i++) { int j = tab[i].indexOf(2); for (int k = j ; k < tab[i].length; k++) { if (tab[i][j] == 2){ tableau.add(j); } } } for (Integer row : tableau) { System.out.println("row = "+ Arrays.toString(tableau)); } } } java ·
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › return-index-of-element-in-java-multidimensional-array-113206
return index of element in java multidimensional array
November 6, 2003 - hi all I cant seem to find any syntax to allow searching or sorting in multidimensional arrays. I have a [4][5] array that i need to sort and then
🌐
Coderanch
coderanch.com › t › 751474 › java › array-return-index-Column-Element
Given a 2D array return the index of a Column where each Element is the same (Beginning Java forum at Coderanch)
May 4, 2022 - A "2D array" is technically an array with elements that are also arrays. So in order to solve your problem you might want to consider the "type" of data you have at different points in your code. Have you written your first if statement yet? If so then look closely because you might find the answer there.
🌐
Codecademy
codecademy.com › learn › learn-java › modules › java-two-dimensional-arrays › cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
In Java, when accessing the element from a 2D array using arr[first][second], the first index can be thought of as the desired row, and the second index is used for the desired column.
🌐
CodeChef Discuss
discuss.codechef.com › general
How to create a Method that would return indices of an element in some 2D array in java. - general - CodeChef Discuss
July 11, 2013 - I am trying to generate a function find_indices(int num) in java which when called with a certain parameter would check the presence of that parameter in a 2D array say K,and would return its indices (indices in the matrix K). I am talking about ...
🌐
w3resource
w3resource.com › java-exercises › search › java-search-exercise-7.php
Java - Find row, column position of a number in a 2D array
* @param row the current row. * @param col the current column. * @param search_element the element that we want to search for. * @return value: If found the index(row and column) of the element. * else return (-1 -1).