简体   繁体   English

如何按降序打印2D数组的值?

[英]How can I print the values of a 2D array in descending order?

So I have a 2D matrix and I'm trying to print out the values from largest to smallest. 所以我有一个2D矩阵,我试图打印出从大到小的值。 I basically do this by always looking for a max and when I find it I set that position equal to 1 in adjacencyMatrix so that we don't count it again. 我基本上都是通过寻找最大值来做到这一点,当我找到它时,我在adjacencyMatrix将该位置设置为1 ,这样我们就不再计算它了。 Problem is when I tested the code it started out correctly by printing out the largest and then skipped the second largest. 问题是当我通过打印出最大的代码然后跳过第二大代码来测试它正确启动的代码。 Then found the 3rd and 4th largest. 然后找到了第3和第4大。 Skipped some more and then finally just started printing out 0s. 跳过了一些,然后最终开始打印出0。

Here is my code: 这是我的代码:

public static void findLongestPath(int rows, int columns, int[][] elevationMatrix, int[][] adjacencyMatrix)
    {
        int max = 0;

        for (int x = 0; x < rows * columns; x++)
        {   
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    if (elevationMatrix[i][j] > max && adjacencyMatrix[i][j] == 0)
                    {
                        max = elevationMatrix[i][j];
                        adjacencyMatrix[i][j] = 1;
                    }
                }
            }

            System.out.println(max);
            max = 0;
        }
    }

I have been staring at it for a while and can't find the bug so I though another pair of eyes might help. 我已经盯着它看了一会儿,找不到这个虫子,所以我觉得另一双眼睛可能有所帮助。

PS Please, please, please don't tell me to sort the array because I can'd do that. 请拜托,请不要告诉我对阵列进行排序,因为我可以这样做。 I need to maintain the order of the original array. 我需要维护原始数组的顺序。

public static void findLongestPath(int rows, int columns, int[][] elevationMatrix, int[][] adjacencyMatrix)
    {
        int max = 0;
        int cX, cY;

        for (int x = 0; x < rows * columns; x++)
        {   
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    if (elevationMatrix[i][j] > max && adjacencyMatrix[i][j] == 0)
                    {
                        max = elevationMatrix[i][j]; // possible max, xth iteration
                        cX = i; // store i
                        cY = j; // store j
                    }
                }
            }

            System.out.println(max); // global max, xth iteration
            max = 0;
            // cX and cJ now point to coordinates of global max
            // all the possible max value coordinates are ignored.
            adjacencyMatrix[cX][cJ] = 1;
        }
    }

I think you need to set adjacencyMatrix[][] = 1 , after you find largest number throughout the matrix(global max), rather than finding a max(possible max). 在您找到整个矩阵中的最大数字(全局最大值)之后,我认为您需要设置adjacencyMatrix[][] = 1 ,而不是找到最大值(可能的最大值)。

There can be more efficient ways to do this, I'm pointing out what needs to be done in this method to work. 可以有更有效的方法来实现这一点,我指出在这种方法中需要做些什么才能工作。

public static void findLongestPath(int rows, int columns, int[][] elevationMatrix)
{
   class PosAndValue implements Comparable<PosAndValue> {
     final int x;
     final int y;
     final int value;
     PosAndValue(int x, int y, int value) {
       this.x = x;
       this.y = y;
       this.value = value;
     }
     public int compareTo(PosAndValue other) {
       return Integer.compare(value, other.value);
     }
   }
   PosAndValue[] array = new PosAndValue[rows * columns];
   for (int i = 0; i < rows; i++) {
     for (int j = 0; j < columns; j++) {
       array[i * columns + j] = new PosAndValue(i, j, elevationMatrix[i][j]);
     }
   }
   Arrays.sort(array);
   for (int i = array.length - 1; i >= 0; i--) {
     System.out.println(array[i].value);
   }
}

The problem is you're trying to pseudo-sort it. 问题是你正在尝试对它进行伪排序。

Just make a list or 1D array of all the values and sort that. 只需创建所有值的列表或一维数组并对其进行排序。 If you declare the temp array in the method, then the garbage collection thread will eventually pick it up. 如果在方法中声明临时数组,那么垃圾收集线程最终会将其拾取。

Your method can be ~10 lines: add all values to the temp array, sort it, print all values. 您的方法可以是~10行:将所有值添加到临时数组,对其进行排序,打印所有值。

int[] tempArr = new int[rows * columns];
for(int i = 0; i < rows; i++){
    for(int j = 0; j < columns; j++){
        tempArr[(i * col) + j] = elevationMatrix[i][j];
    }
}
Arrays.sort(tempArr);
for(int x = (rows * columns) - 1; x >= 0; x--){
    System.out.println(tempArr[x]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM