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

时间:2011-09-23 22:43:57

标签: java

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

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 

9 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

小心你的阵列尺寸!大多数人的第二个陈述是错误的。它应该达到 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;
        }
    }
}

答案 2 :(得分:1)

每当您更新最新

时,请存储i,j

答案 3 :(得分:1)

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

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

答案 4 :(得分: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]);
}

答案 5 :(得分: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); 

答案 6 :(得分: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;
                    }

答案 7 :(得分: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+"]");

答案 8 :(得分:0)

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

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

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