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

时间:2016-10-08 20:12:37

标签: java arrays

所以我有一个2D矩阵,我正在尝试从最大到最小打印出值。我基本上是通过寻找最大值来做到这一点,当我找到它时,我将该位置设置为1中的adjacencyMatrix,以便我们不再计算它。问题是当我通过打印出最大的代码然后跳过第二大代码来测试它正确启动的代码。然后找到了第3和第4大。跳过了一些,然后最终开始打印出0。

这是我的代码:

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;
        }
    }

我已经盯着它看了一会儿,找不到这个虫子,所以我觉得另一双眼睛可能有所帮助。

P.S。拜托,请不要告诉我对阵列进行排序,因为我可以这样做。我需要保持原始数组的顺序。

3 个答案:

答案 0 :(得分:1)

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;
        }
    }

我认为您需要在整个矩阵(全局最大值)中找到最大数字后设置adjacencyMatrix[][] = 1,而不是找到最大值(可能的最大值)。

可以有更有效的方法来实现这一点,我指出在这种方法中需要做些什么才能工作。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

问题是你正试图对它进行伪排序。

只需创建所有值的列表或一维数组并对其进行排序。如果在方法中声明临时数组,那么垃圾收集线程最终会将其取出。

您的方法可以是~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]);
}
相关问题