如何从二维数组中查找索引

我要做的是打印二维数组中的最大数字,它是索引位置。 我能找到最大的数字,但我似乎无法弄清楚如何打印它的索引位置。 无论如何,这是我到目前为止所拥有的:

public static void main(String[] args) { int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}}; double max = arr[0][0]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j  max) { max = arr[i][j]; } } } System.out.println(max); System.out.println(i + j); //No idea what I should be doing here, just trying out everything I can think of 

现在,你应该始终得到2 * arr.length作为最终值。 这不是你可能想要的。 看起来您想知道最大值的坐标。 为此,您需要缓存索引的值,然后再使用它们:

 public static void main(String[] args) { int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}}; int tmpI = 0; int tmpJ = 0; double max = arr[0][0]; // there are some changes here. in addition to the caching for (int i = 0; i < arr.length; i++) { int[] inner = arr[i]; // caches inner variable so that it does not have to be looked up // as often, and it also tests based on the inner loop's length in // case the inner loop has a different length from the outer loop. for (int j = 0; j < inner.length; j++) { if (inner[j] > max) { max = inner[j]; // store the coordinates of max tmpI = i; tmpJ = j; } } } System.out.println(max); // convert to string before outputting: System.out.println("The (x,y) is: ("+tmpI+","+tmpJ+")"); 

小心你的arrays尺寸! 你们大多数人的第二个陈述是错误的。 它应该达到arr [i] .length

 for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { if (arr[i][j] > max) { max = arr[i][j]; tmpI = i; tmpJ = j; } } } 

每当更新最大值时存储i,j

如果你想要一个扁平数组的索引,那就是这样:

 public static void main (String[] args) throws java.lang.Exception { int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}}; int[] flattened = new int[6*3]; // based off above int maxIndex = 0; double max = arr[0][0]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { flattened[i + j] = arr[i][j]; if (arr[i][j] > max) { max = arr[i][j]; maxIndex = i+j; } } } System.out.println(max); System.out.println(flattened [maxIndex]); } 
 int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}}; int max = arr[0][0]; int maxI = 0, maxJ = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i][j] > max) { max = arr[i][j]; maxI = i; maxJ = j; } } } System.out.println(max); System.out.println(maxI + "," + maxJ); 

你有一个二维数组,因此你需要知道两个索引。 将它们加在一起是不行的,因为你失去了哪个是哪个。 这个怎么样:

 System.out.println("[" + i + "][" + j + "]"); 
 //C++ code #include #include #include using namespace std; vector b; vector c; int Func(int a[][10],int n) { int max; max=a[0][0]; for(int i=0;imax) { max=a[i][j]; b.push_back(i); c.push_back(j); } } } b.push_back(0); c.push_back(0); return max; } void display(int a[][10],int n) { for(int i=0;i>n; for(int i=0;i>a[i][j]; } } cout< 

您只需将索引i和j一起添加,然后将其打印到屏幕上即可。 由于你正在运行整个循环,它将等于2 * arr.length-2。 您需要做的是在遇到新的最大值时存储i和j的值。

例如:

 int[][] arr = {{4, 44, 5, 7, 63, 1}, {7, 88, 31, 95, 9, 6}, {88, 99, 6, 5, 77, 4}}; int max = arr[0][0]; //dunno why you made it double when you're dealing with integers int max_row=0; int max_column=0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (arr[i][j] > max) { max = arr[i][j]; max_row=i; max_column=j; } } System.out.println("The max is: "+max+" at index ["+max_row+"]["+max_column+"]"); 

不确定是否实现了有效的算法,但是为什么在设置max时只是不将索引i,j保存在另一个变量中。 这很简单。

 if (arr[i][j] > max) { max = arr[i][j]; maxX = i; maxY = j; } 

仅供参考如果您想要更好的实施,请查看“插入排序”算法。