Here is a very good explanation about Java 2D arrays
int num[][] = {{4,5,6},{8,3,1},{7,8,9}}; int N = 5; int result[] = new int[num.length]; for(int i=0; i<num.length; i++){ result[i] = -1; for(int j=0; j<num[0].length; j++){ if( N < num[i][j] ){ result[i] = j; break; } } } for(int i=0; i<result.length; i++){ System.out.println(result[i]); }
The first for loop (one with a for inside it) intersects the 2D array from top to bottom in a left to right direction. This is, firstly, this is with 4, then 5,6,8,3,1,7,8,9.
First, an array of results is created. The length depends on the number of lines num. the result [i] is set to -1 if the number is not greater than N. if the number is greater than N is found, the column index is saved as the result [i] = j, and a gap is used to exit the for loop, since we just want to find the index of the first number, greater than N.
The last for loop just prints the result.
source share