我有一个每秒更新一次的 ArrayList 以进行一些基本检查并维护当前满足一组条件的玩家列表。
我想知道执行此操作的最高性能方法是我有 2 个建议的解决方案。
public void update() {
for (Player player : Bukkit.getOnlinePlayers()) {
if (!playersOnLadder.contains(player)) {
if (checkPlayerOnLadder(player)) {
playersOnLadder.add(player);
}
} else {
if (checkPlayerOnLadder(player)) {
playersOnLadder.remove(player);
}
}
}
}
public void update() {
playersOnLadder.clear();
for (Player player : Bukkit.getOnlinePlayers()) {
if (checkPlayerOnLadder(player)) {
playersOnLadder.add(player);
}
}
}
在任何给定时间,这个数组列表中通常会有大约 75 名玩家。“在梯子上检查玩家的方法如下所示:
private boolean checkPlayerOnLadder(Player player) {
int ladderAbsolute = this.getX()+this.getZ();
int playerAbsolute = (int) player.getLocation().getX()+ (int) player.getLocation().getZ();
//If the player is within 4 blocks (2 in each direction) of the ladder then return true.
if (ladderAbsolute == playerAbsolute || (ladderAbsolute-2 > playerAbsolute && ladderAbsolute+2 < playerAbsolute)) {
return true;
} else {
return false;
}
}
EDITED 转换为 HashSet