初始化2d java数组
我正在做一个非常基本的游戏。但是,当我尝试创建数组时,我遇到了错误。该错误超出了索引范围。但是我想我通过添加-1来修正它,以确保我不会越界。有人能告诉我,或者给我一个关于我做错了什么的线索吗初始化2d java数组,java,arrays,initialization,Java,Arrays,Initialization,我正在做一个非常基本的游戏。但是,当我尝试创建数组时,我遇到了错误。该错误超出了索引范围。但是我想我通过添加-1来修正它,以确保我不会越界。有人能告诉我,或者给我一个关于我做错了什么的线索吗 package gameProject; public class world { int numEnemies, numBosses; int [][] world = new int[10][10]; public world(){ int[][] world = createArray(
package gameProject;
public class world {
int numEnemies, numBosses;
int [][] world = new int[10][10];
public world(){
int[][] world = createArray(10,10);
populateWorld(world);
}
private int[][] createArray(int inX, int inY){
//create the array that holds world values
int[][] world = new int[inX][inY];
//initialize the world array
for(int i = 0; i < world.length - 1; i ++){
for(int j = 0; j < world[0].length - 1; j++){
world[i][j] = 0;
}
}
return world;
}
private void populateWorld(int[][] world){
for(int i = 0; i < world.length - 1; i++){
for(int j = 0; j < world[0].length - 1; i++){
world[i][j] = 0;
}
}
}
}
package项目;
公共阶级世界{
在numEnemies,numBosses;
int[][]世界=新int[10][10];
公共世界(){
int[][]world=createArray(10,10);
大众世界(世界);
}
私有int[][]创建数组(int-inX,int-inY){
//创建保存世界值的数组
int[][]世界=新int[inX][inY];
//初始化世界数组
for(int i=0;i
在您的populateWorld
方法中,更改
for(int j = 0; j < world[0].length - 1; i++)
for(int j=0;j
到
for(int j=0;j
您不断增加错误的计数器,最终超出其界限。(10)
(PS:在循环条件下不需要
length-1
,只要length
就可以了)在populateWorld
方法中,更改
for(int j = 0; j < world[0].length - 1; i++)
for(int j=0;j
到
for(int j=0;j
您不断增加错误的计数器,最终超出其界限。(10)
(PS:在循环条件下不需要length-1
,只要length
就可以了)错误在
for (int j = 0; j < world[0].length - 1; i++)
for(int j=0;j
你应该写
for (int j = 0; j < world[0].length - 1; j++)
for(int j=0;j
相反
请注意,您可以稍微减少代码:
您为成员World.World
创建了两次数组。此外,int数组的元素已初始化为0,因此无需显式执行此操作。错误在
for (int j = 0; j < world[0].length - 1; i++)
for(int j=0;j
你应该写
for (int j = 0; j < world[0].length - 1; j++)
for(int j=0;j
相反
请注意,您可以稍微减少代码:
您为成员World.World
创建了两次数组。此外,int数组的元素已初始化为0,因此不需要显式执行此操作。您只需执行此操作即可
private int[][] createArray(int inX, int inY) {
int[][] world = new int[inX][inY];
for (int i = 0; i < inX; i++)
for (int j = 0; j < inY; j++)
world[i][j] = 0;
return world;
}
private int[][]创建数组(int-inX,int-inY){
int[][]世界=新int[inX][inY];
对于(int i=0;i
实际上,您永远不需要检查world数组的长度,因为该长度已经作为参数值传入
然后也
private void populateWorld(int[][] world) {
for (int i = 0; i < world.length; i++)// v error 'i' should be 'j'
for (int j = 0; j < world[i].length; j++) // <- error on this line
world[i][j] = 0;
}
private void populateWorld(int[]]world){
对于(int i=0;i
private int[][] createArray(int inX, int inY) {
int[][] world = new int[inX][inY];
for (int i = 0; i < inX; i++)
for (int j = 0; j < inY; j++)
world[i][j] = 0;
return world;
}
private int[][]创建数组(int-inX,int-inY){
int[][]世界=新int[inX][inY];
对于(int i=0;i
实际上,您永远不需要检查world数组的长度,因为该长度已经作为参数值传入
然后也
private void populateWorld(int[][] world) {
for (int i = 0; i < world.length; i++)// v error 'i' should be 'j'
for (int j = 0; j < world[i].length; j++) // <- error on this line
world[i][j] = 0;
}
private void populateWorld(int[]]world){
对于(int i=0;i 对于(int j=0;j来说,您的基本问题是递增了错误的循环变量。
为什么?因为你远离任何干净的代码。
让我向您展示如何进行干净的编码:
- 类名以大写字母开头,方法和变量名以小写字母开头
- 您可以使用变量的作用域作为变量的前缀(“m”表示成员,“p”表示参数,不表示局部变量)。保存对“this”的所有时间引用。这在很大程度上取决于您的代码样式。我强烈建议您这样做,使您的代码保持干净,并且非常易于调试
- 尽可能使用final和private关键字
- 使用描述性变量名。这里特别是x和y表示循环变量,因为您正在抽象二维平面
还有一些考虑:
- 通常游戏会变得更复杂。通常简单的原语(比如你的int数组)不足以长时间存储所有相关信息。使用像Cell这样的类
- 使用枚举可以丢失神奇的数字=>编码、读取和调试变得容易多了
因此,经过大量讨论后,这里是最终代码:
package gameproject;
/**
* Use comments like this to describe what the classes purpose is.
* Class comment is the most important one. If you can't tell what a method/variable is doing by its name, you should also comment methods and/or variables!
* @author JayC667
*/
public class World {
/*
* STATIC part of the class - keep separated from object code
*/
// you could/should also put these static classes to their separate files and make em non-static
/**
* Denotes, what a {@linkplain Cell} is occupied with
*/
static public enum CellType {
EMPTY, //
BOSS, //
ENEMY
}
/**
* Represents a single cell within the world. Stores information about its coodrinates (redundant) and its occupator (see {@linkplain CellType})
* @author JayC667
*/
static private class Cell { // use cell to store data for you
public final int mX; // x and y are actually not useful at the moment, you could also remove them
public final int mY;
private CellType mCellType = CellType.EMPTY;
public Cell(final int pX, final int pY) {
mX = pX;
mY = pY;
}
public CellType getCellType() {
return mCellType;
}
public void setCellType(final CellType pCellType) {
mCellType = pCellType;
}
}
// when possible, make methods static, unless you unnecessarily blow up the parameter list
// this is a typical demo for a factory method
static private Cell[][] createWorld(final int pWidth, final int pHeight) {
final Cell[][] newWorld = new Cell[pWidth][pHeight];
for (int y = 0; y < pHeight - 1; y++) {
for (int x = 0; x < pWidth - 1; x++) {
newWorld[y][x] = new Cell(x, y);
}
}
return newWorld;
}
/*
* OBJECT part of the class - keep separated from static code
*/
private final Cell[][] mWorld;
private final int mWorldWidth;
private final int mWorldHeight;
private final int mNumberOfEnemies;
private final int mNumberOfBosses;
public World(final int pWidth, final int pHeight, final int pNumberOfEnemies, final int pNumberOfBosses) {
if (pWidth < 1 || pHeight < 1) throw new IllegalArgumentException("World width and height must be greater than 0!");
if (pNumberOfEnemies < 0 || pNumberOfBosses < 0) throw new IllegalArgumentException("Enemy and boss counts must not be negative!");
if (pWidth * pHeight < pNumberOfEnemies + pNumberOfBosses) throw new IllegalArgumentException("World is too small for all the bad guys!");
mWorldWidth = pWidth;
mWorldHeight = pHeight;
mNumberOfEnemies = pNumberOfEnemies;
mNumberOfBosses = pNumberOfBosses;
mWorld = createWorld(pWidth, pHeight);
populateWorld();
}
// refers to many member variables, so not static (would only blow up parameter list)
private void populateWorld() {
for (int i = 0; i < mNumberOfBosses; i++) {
final Cell c = getRandomCell(CellType.EMPTY);
mWorld[c.mY][c.mX].setCellType(CellType.BOSS);
}
for (int i = 0; i < mNumberOfEnemies; i++) {
final Cell c = getRandomCell(CellType.EMPTY);
mWorld[c.mY][c.mX].setCellType(CellType.ENEMY);
}
}
private Cell getRandomCell(final CellType pCellType) {
while (true) { // TODO not a good, but simple solution; might run infinite loops
final int randomX = (int) (mWorldWidth * Math.random());
final int randomY = (int) (mWorldHeight * Math.random());
if (mWorld[randomY][randomX].getCellType() == pCellType) return new Cell(randomX, randomY);
}
}
}
package项目;
/**
*使用这样的注释来描述类的用途。
*类注释是最重要的一个。如果你不能通过名称来判断一个方法/变量在做什么,你也应该注释方法和/或变量!
*@作者JayC667
*/
公共阶级世界{
/*
*类的静态部分-与目标代码保持分离
*/
//您还可以/应该将这些静态类放在各自的文件中,并使它们成为非静态的
/**
*表示{@linkplain Cell}被占用的内容
*/
静态公共枚举单元类型{
空的,//
老板//
敌人
}
/**
*表示世界中的单个单元格。存储有关其坐标系(冗余)及其占用者的信息(请参见{@linkplain CellType})
*@作者JayC667
*/
静态私有类单元格{//使用单元格为您存储数据
public final int mX;//x和y实际上在mom中没有用处