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

时间:2015-06-07 01:39:01

标签: java arrays class object

我正在研究一个体素引擎,目前我仍然坚持创建块。你可以忽略关于LWJGL / OpenGL的部分,这不是问题。当我尝试创建块时,我得到了一堆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();
                }

            }

        }

    }
}

}

这是块类:

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


}

}

这是Saving类(忽略这个):

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

}

}

1 个答案:

答案 0 :(得分:1)

要么我累了,要么答案很简单。实例化多维数组,最后3个维度的长度为零。然而,在下一步中,您尝试从这些维度访问数据。让我试着简化你的案例。你在做什么等同于

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

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