Java索引越界错误

时间:2013-10-06 20:51:16

标签: java runtime-error indexoutofboundsexception

我正在用Java编写代码作为Minecraft服务器的插件,但逻辑原则本质上是通用的。

public void doReviewMember(CommandSender playerSent) {
    if (!reviewsMember.isEmpty()) {
        Review doThis = null;

        ArrayList<Review> players = new ArrayList<Review>();
        ArrayList<Review> playersVIP = new ArrayList<Review>();
        ArrayList<Review> playersVIPplus = new ArrayList<Review>();

        for (int c1 = 0; c1 < reviewsMember.size(); c1++) {
            if  (Bukkit.getPlayer(reviewsMember.get(c1).getName()).hasPermission("reviewplugin.vipplus"))
                playersVIPplus.add(reviewsMember.get(c1));
            else if (Bukkit.getPlayer(reviewsMember.get(c1).getName()).hasPermission("reviewplugin.vip"))
                playersVIP.add(reviewsMember.get(c1));
            else players.add(reviewsMember.get(c1));
        }

        if (playersVIPplus.size() > 0)
            doThis = playersVIPplus.get(0);
        else if (playersVIP.size() > 0)
            doThis = playersVIP.get(0);
        else doThis = players.get(0);

        Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThis review is for §b" + doThis.getName());
        Bukkit.getPlayer(playerSent.getName()).teleport(doThis.getLocation());
        reviewsMember.remove(doThis);
        if (reviewsMember.size() > 1)
            Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThere are " + reviewsMember.size() + " member reviews left to do.");
        else if (reviewsMember.size() == 1)
            Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThere is " + reviewsMember.size() + " member review left to do.");
        else
            Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThere are no more Member reviews to do at this time!");
    }
    else {
        Bukkit.getPlayer(playerSent.getName()).sendMessage("§4[§6ReviewPlugin§4] §eThere are no more Member reviews to do at this time!");
    }
}

索引越界错误发生在for循环中,所以我不知道我哪里出错了。这是在Windows 8计算机上没有错误的测试,但是当在Linux中实现时,每次索引越界错误都会失败。

1 个答案:

答案 0 :(得分:0)

上述代码不能失败除非某些后台线程在循环中间修改数组。这很可能是您遇到问题的原因。