![](/img/trans.png)
[英]java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
[英]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 開始的,有效值為0
到size() - 1
。 當你生成一個新的隨機數時,你不應該+ 1
你想要一個0
到size() -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++) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.