【问题标题】:java.lang.IndexOutOfBoundsException: Index: 5, Size: 5java.lang.IndexOutOfBoundsException:索引:5,大小:5
【发布时间】:2017-11-03 07:05:26
【问题描述】:

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

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);
    }

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

【问题讨论】:

  • get() 上的索引值是从零开始的,那么为什么要加 1? Javadoc 甚至说:如果索引超出范围 (index = size()),则抛出 IndexOutOfBoundsException
  • 在此处查看其他提供的答案
  • 假设你修改for循环从0开始。
  • @RajithPemabandu 你什么意思???
  • @MatthewParks 只做int n = rand.nextInt(drops.size())。这应该足够了。

标签: java arrays indexing forge


【解决方案1】:

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

【讨论】:

  • 随机部分的加1?
  • 是的。 rand.nextInt(size()) 为您提供介于 0size() - 1 之间的数字,这正是您所需要的。
  • 所以如果我把它改成这样: int n = rand.nextInt(drops.size()) - 1;它应该工作正常吗?
  • 它将修复索引越界错误(无需-1)。但是我不知道您从1&lt; size() - 1 的循环的意图是什么,因为&lt; 而不是&lt;=,它将忽略第一个单元格0 和最后一个单元格size() - 1
  • @MatthewParks 不,如果您只是从rand.nextInt(drops.size()) 中删除+ 1,它会起作用。
【解决方案2】:

我在使用数组时遇到了类似的问题。我相信它与 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++) {

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多