我完全不知道如何解决这个问题,但我正在使用 Java 中的 AI 寻路系统的数组,并且需要对值进行排序。这是我生成的数组:
int[][] paths = new int[Settings.GAME_COLS][Settings.GAME_ROWS]; // Just creating an Array that has room for every tile in the Game, so the Array can be used like this: paths[x][y]
int tempF = 0;
tempF = Math.abs(14 + (((aiX + 1) - playerX) + ((aiY - 1) - playerY)));
paths[aiX + 1][aiY - 1] = tempF;
tempF = Math.abs(14 + (((aiX + 1) - playerX) + ((aiY + 1) - playerY)));
paths[aiX + 1][aiY + 1] = tempF;
tempF = Math.abs(14 + (((aiX - 1) - playerX) + ((aiY + 1) - playerY)));
paths[aiX - 1][aiY + 1] = tempF;
tempF = Math.abs(14 + (((aiX - 1) - playerX) + ((aiY - 1) - playerY)));
paths[aiX - 1][aiY - 1] = tempF;
tempF = Math.abs(10 + (((aiX + 1) - playerX) + (aiY - playerY)));
paths[aiX + 1][aiY ] = tempF;
tempF = Math.abs(10 + (((aiX - 1) - playerX) + (aiY - playerY)));
paths[aiX - 1][aiY ] = tempF;
tempF = Math.abs(10 + ((aiX - playerX) + ((aiY + 1) - playerY)));
paths[aiX ][aiY + 1] = tempF;
tempF = Math.abs(10 + ((aiX - playerX) + ((aiY - 1) - playerY)));
paths[aiX ][aiY - 1] = tempF;
这一切都可以完美地查找并生成包含所需信息的数组,但我需要根据添加到数组中的“tempF”值对数组进行排序。有没有简单的方法可以做到这一点?
最佳答案
多维数组只不过是一组数组,因此您可以使用常规
Arrays.sort
方法将就地对数组数组进行排序。
关于Java 多维数组排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269313/