gpt4 book ai didi

java.lang.ArrayIndexOutOfBoundsException ,无法创建对象数组

转载 作者:行者123 更新时间:2023-12-01 11:27:43 24 4
gpt4 key购买 nike

我正在开发体素引擎,目前我陷入了 block 的创建。你可以忽略关于LWJGL/OpenGL的部分,那不是问题。当我尝试创建 block 时,我收到一堆 java.lang.ArrayIndexOutOfBoundsException 错误。

package LWJGL.TESTS.WORLS;

import java.util.Random;


public class Chunck {

private int x, y, z, id;

Chunck(int id, int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
this.id = id;
}

这是不起作用的部分,没有帖子能够回答修复:

`   void loadChunck(){
//x
for(int x = 1; x < 16; x++){
//z
for(int z = 1; z < 16; z++){
//y
for(int y = 1; y < 128; y++){
try{
Block[][][][][][] blockObject = new Block[16][16][128][0][0][0];
blockObject[x][y][z][this.x][this.y][this.z] = new Block(x, y, z, this.x, this.y, this.z, String.valueOf(x)+String.valueOf(y)+String.valueOf(z), this.id);
}catch(java.lang.ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}

}

}

}
}

}

这是 block 类:

package LWJGL.TESTS.WORLS;

import org.lwjgl.opengl.GL11;

public class Block {

private int x, y, z, cx, cy, cz, cID;
String ID;

Block(int x, int y, int z, int cx, int cy, int cz, String ID, int cID){

this.x = x;
this.y = y;
this.z = z;
this.cx = cx;
this.cy = cy;
this.cz = cz;
this.ID = ID;
this.cID = cID;
System.out.println("Block ID: " + ID);
System.out.println("Block Chunck ID: " + cID);
System.out.println("Block X: " + x);
System.out.println("Block Chunck X: " + cx);
System.out.println("Block Y: " + y);
System.out.println("Block Chunck Y: " + cy);
System.out.println("Block Z: " + z);
System.out.println("Block Chunck Z: " + cz);
}
private Block[][][][][][] InitBlock(){


return null;

}
public void render(){

GL11.glBegin(GL11.GL_QUADS);
GL11.glColor3f(1.0f,1.0f,0.0f);
GL11.glVertex3f(this.x, this.y,-1*this.z);
GL11.glVertex3f(-1*this.x, this.y,-1*this.z);
GL11.glVertex3f(-1*this.x, this.y, this.z);
GL11.glVertex3f(this.x, this.y, this.z);
GL11.glColor3f(this.x,0.5f,0.0f);
GL11.glVertex3f(this.x,-1*this.y, this.z);
GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
GL11.glColor3f(1.0f,0.0f,0.0f);
GL11.glVertex3f(this.x, this.y, this.z);
GL11.glVertex3f(-1*this.x, this.y, this.z);
GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
GL11.glVertex3f(this.x,-1*this.y, this.z);
GL11.glColor3f(1.0f,1.0f,0.0f);
GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
GL11.glVertex3f(-1*this.x, this.y,-1*this.z);
GL11.glVertex3f(this.x, this.y,-1*this.z);
GL11.glColor3f(0.0f,0.0f,1.0f);
GL11.glVertex3f(-1*this.x, this.y, this.z);
GL11.glVertex3f(-1*this.x, this.y,-1*this.z);
GL11.glVertex3f(-1*this.x,-1*this.y,-1*this.z);
GL11.glVertex3f(-1*this.x,-1*this.y, this.z);
GL11.glColor3f(1.0f,0.0f,1.0f);
GL11.glVertex3f(this.x, this.y,-1*this.z);
GL11.glVertex3f(this.x, this.y, this.z);
GL11.glVertex3f(this.x,-1*this.y, this.z);
GL11.glVertex3f(this.x,-1*this.y,-1*this.z);
GL11.glEnd();


}

}

这是 Saves 类(忽略它):

package LWJGL.TESTS.WORLS;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Saves {

void Save(){

PrintWriter writer = null;
try {
writer = new PrintWriter("data.txt", "UTF-8");
} catch (FileNotFoundException | UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer.print("");
writer.close();
}

void Load() throws FileNotFoundException{

Scanner scanner = new Scanner(new File("data.txt"));
while(scanner.hasNextInt()){
System.out.println(scanner.nextInt());

}


}

}

最后这是主类,如果这篇文章真的很长,抱歉:

package LWJGL.TESTS.WORLS;

import java.io.FileNotFoundException;

public class Main {

public static void main(String[] args) throws FileNotFoundException {
Saves load = new Saves();
Chunck chunck = new Chunck(1, 0, 0, 0);
chunck.loadChunck();
load.Save();
load.Load();

}

}

最佳答案

要么我累了,要么答案很简单。您的多维数组被实例化,最后 3 个维度的长度为零。然而,在下一步中,您将尝试访问这些维度的数据。让我尝试简化你的情况。你正在做的事情相当于

Block[] blockObject = new Block[0];
blockObject[x] = new Block(...);

在这种情况下,访问 blockObject[x] 将触发异常,因为无论 x 是什么,数组中都没有元素。您的数组长度为零。

关于java.lang.ArrayIndexOutOfBoundsException ,无法创建对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com