【问题标题】:java.lang.ArrayIndexOutOfBoundsException , Array of objects can't be createdjava.lang.ArrayIndexOutOfBoundsException , 无法创建对象数组
【发布时间】:2015-06-07 01:39:00
【问题描述】:

我正在开发体素引擎,目前我正忙于创建块。您可以忽略有关 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();    


}

}

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

}

}

【问题讨论】:

  • 具有这些维度的数组:new Block[16][16][128][0][0][0],任何元素的空间为零,并且您永远不能向其中分配任何内容。你想做什么?
  • 你为什么要创建一个数组(一个实际上不能保存任何东西的数组),给它分配一个值,然后让它超出范围而不使用它?除非我遗漏了什么,否则真的不清楚你到底想做什么。
  • 我相信 [16][16][128] 是块中的块位置,而 [0][0][0] 是块位置,这使得这更加奇怪,因为即使数组不是空的,值都是this.x,this.y,this.z,但他还是直接在块上设置块坐标。对您要完成的工作进行一些说明会很好。

标签: java arrays class object


【解决方案1】:

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

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

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

【讨论】:

  • 感谢您的基本概念。它导致我弄乱了一些数字并得到了这个:try{ Block[][][][][][] blockObject = new Block[16][16][128][16][16][16]; 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(); } 这完全解决了问题。
猜你喜欢
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 2010-12-25
  • 2014-11-07
相关资源
最近更新 更多