[英]java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
I am having an issue with arrays.我遇到了数组问题。 The full stack trace is:
完整的堆栈跟踪是:
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/:?]
The code I use for this is:我为此使用的代码是:
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);
This code calls DropDB and selects a random drop from a registered list.此代码调用 DropDB 并从注册列表中选择一个随机丢弃。 The list is perfectly fine.
名单完全没问题。 Here is the code for getDrop is:
这是 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);
}
This code causes this error.此代码导致此错误。 I have tired to look at the other questions around here.
我已经厌倦了看这里的其他问题。 They have not worked.
他们没有工作。
Indices in Java are 0-based the valid values are 0
to size() - 1
. Java 中的索引是从 0 开始的,有效值为
0
到size() - 1
。 When you generate a new random number you should not + 1
you want a range of 0
to size() -1
.当你生成一个新的随机数时,你不应该
+ 1
你想要一个0
到size() -1
。
I was having similar issues with an array.我在数组上遇到了类似的问题。 I believe it has to do with the for loop itself, quite not sure though, feel free to correct.
我相信这与 for 循环本身有关,但不确定,请随时纠正。 The equivalent of what solved my issue would be this :
解决我的问题的等价物是这样的:
Look at this part看看这部分
for (int i = 1; i < drop.getDrops().size() -1; i++)
I would do this instead :我会这样做:
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++) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.