2011-09-23 244 views
2

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

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 < arr.length; j++) { 
      if (arr[i][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 

回答

1

存储我,j当你更新最大。

1

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

System.out.println("[" + i + "][" + j + "]"); 
3

现在,你应该始终得到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+")"); 
+0

非常感谢。我在过去的两个小时里吓了一跳..... 我其实尝试了类似于tmpI和tmpJ的东西,但没有成功。我和你所做的事情之间的区别在于,我没有在开始时将该值设置为0。 – MNX1024

+0

@ MNX1024适用于我们所有人。 – cwallenpoole

+0

刚刚意识到你编辑的代码。你原来是什么,我正在寻找。我在这里发布的只是一个测试,我正在做的是在修改它并在另一个程序中使用它之前正确地运行代码。无论如何,我还有一个问题。如果我要将它放入方法中,并且希望在单个return语句中返回i和j。可能吗?如果是的话,我该怎么做? – MNX1024

1

这将是,如果你想要一个索引到一个扁平化阵列:

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]); 
} 
0

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

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

供参考如果你想看看“插入排序”算法,如果你想更好的实施。

2

小心你的数组尺寸!你们大多数人的第二个陈述是错误的。它应该去达改编[I]。长度

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; 
     } 
    } 
} 
1
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); 
1
//C++ code 
#include<iostream> 
#include<vector> 
#include<algorithm> 
using namespace std; 
vector<int> b; 
vector<int> c; 
int Func(int a[][10],int n) 
{ 
    int max; 
    max=a[0][0]; 
    for(int i=0;i<n;i++) 
    { 
      for(int j=0;j<n;j++) 
      { 
        if(a[i][j]>max) 
        { 
            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;i++) 
    { 
      for(int j=0;j<n;j++) 
      { 
        cout<<a[i][j]<<"\t"; 
        } 
        cout<<endl; 
        } 
        } 

int main() 
{ 
    int a[10][10],n; 
    cin>>n; 
    for(int i=0;i<n;i++) 
    { 
      for(int j=0;j<n;j++) 
      { 
        cin>>a[i][j]; 
        } 
        } 
        cout<<endl; 
        display(a,n); 
        cout<<endl; 
        cout<<Func(a,n)<<" is the greatest "<<endl; 
        if(b.size()==1&&c.size()==1) 
        { 
              cout<<"Location is (1,1)"<<endl; 
              } 
              else 
              { 
               b.erase(b.end() - 1); 
               c.erase(c.end() - 1); 
        cout<<"Location is "<<"("<<b.back()+1<<","<<c.back()+1<<")"<<endl; 
        } 
        return 0; 
        } 
+0

我不知道你为什么在java问题上发布C++代码,除非你只是想进一步迷惑他... – sreya

+0

这是一个java问题。将来,请使用一般概念或Java回答java问题。 –

1

你只是增加指数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+"]");