gpt4 book ai didi

Java 多维数组排序

转载 作者:行者123 更新时间:2023-12-02 07:35:41 24 4
gpt4 key购买 nike

我完全不知道如何解决这个问题,但我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com