java.lang.IndexOutOfBoundsException:索引:5,大小:5

java.lang.IndexOutOfBoundsException: Index: 5, Size: 5

我遇到数组问题。完整的堆栈跟踪是:

java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
    at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.7.0_79]
    at java.util.ArrayList.get(Unknown Source) ~[?:1.7.0_79]
    at xyz.lexium.brocubes.drops.DropDB.getRandomDrop(DropDB.java:17) ~[DropDB.class:?]
    at xyz.lexium.brocubes.blocks.BroBlock.onBlockDestroyedByPlayer(BroBlock.java:33) ~[BroBlock.class:?]
    at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDestroyBlock(PlayerControllerMP.java:187) ~[PlayerControllerMP.class:?]
    at net.minecraft.client.multiplayer.PlayerControllerMP.func_178891_a(PlayerControllerMP.java:68) ~[PlayerControllerMP.class:?]
    at net.minecraft.client.multiplayer.PlayerControllerMP.func_180511_b(PlayerControllerMP.java:232) ~[PlayerControllerMP.class:?]
    at net.minecraft.client.Minecraft.clickMouse(Minecraft.java:1519) ~[Minecraft.class:?]
    at net.minecraft.client.Minecraft.runTick(Minecraft.java:2126) ~[Minecraft.class:?]
    at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1087) ~[Minecraft.class:?]
    at net.minecraft.client.Minecraft.run(Minecraft.java:376) [Minecraft.class:?]
    at net.minecraft.client.main.Main.main(Main.java:117) [Main.class:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_79]
    at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
    at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_79]
    at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source) [start/:?]
    at GradleStart.main(Unknown Source) [start/:?]

我为此使用的代码是:

DropBase drop = DropDB.getRandomDrop();
for (int i = 1; i < drop.getDrops().size() -1; i++) {
    EntityItem item = new EntityItem(worldIn, pos.getX(), pos.getY() + 1, pos.getZ(), drop.getDrops().get(i));
    System.out.println(i);
    worldIn.spawnEntityInWorld(item);

此代码调用 DropDB 并从注册列表中随机选择一个下拉列表。该列表非常好。这是 getDrop 的代码:

 public static DropBase getRandomDrop() {
     Random rand = new Random();
        int n = rand.nextInt(drops.size()) + 1;
        System.out.println(n);
        System.out.println(drops.size());
        return drops.get(n);
    }

此代码导致此错误。我已经厌倦了看这里的其他问题。他们没有工作。

Java 中的索引从 0 开始,有效值为 0size() - 1。当你生成一个新的随机数时,你不应该 + 1 你想要的范围是 0size() -1.

我在数组方面遇到了类似的问题。我相信它与 for 循环本身有关,但不太确定,请随时更正。 解决我的问题的等效方法是:

看这部分

for (int i = 1; i < drop.getDrops().size() -1; i++) 

我会这样做:

Int dropsSize = drop.getDrops().size() - 1;  // just to keep it clean 
                                             // but you don't have to do this.
for (int **i = 0**; i < dropsSize ; i++) {