简体   繁体   English

二维整数数组不接受我提供的尺寸

[英]Two-dimensional int array is not accepting the dimensions I am providing

This is the code that is in my GridGenerator class. 这是我的GridGenerator类中的代码。 The purpose is to create multiple rectangular rooms that can eventually be joined together into a map. 目的是创建多个矩形房间,最终可以将它们连接在一起成为地图。

int xRange, yRange;

//constructor
public GridGenerator(int xInput, int yInput) {
    xRange = xInput;
    yRange = yInput;
}

int[][] grid = new int[yRange][xRange];
//the first number indicates the number of rows, the second number indicates the number of columns
//positions dictated with the origin at the upper-left corner and positive axes to bottom and left

void getPosition(int x, int y) {
    int position = grid[y][x]; //ArrayIndexOutOfBoundsException here
    System.out.println(position);
}

This is the code that is in my MapperMain class. 这是我的MapperMain类中的代码。 The purpose is to join GridGenerator instances into a multi-room map. 目的是将GridGenerator实例连接到多房间地图中。 I'm also using it for debugging and scaffolding purposes for now. 我现在也将其用于调试和脚手架目的。

public static void main(String[] args) {

    GridGenerator physicalLayer1 = new GridGenerator(10,15);

    physicalLayer1.getPosition(0, 0); //ArrayIndexOutOfBoundsException here

}

I'm getting an ArrayIndexOutOfBoundsException error. 我收到ArrayIndexOutOfBoundsException错误。 At some point, xRange is assigned a value of 10 and yRange is assigned a value of 15. However, when I attempt to use xRange and yRange as the parameters for grid , Java has some problem with that and I'm not sure why. 在某个时候, xRange被赋值为10, yRange被赋值为15。但是,当我尝试将xRangeyRange用作grid的参数时,Java对此存在一些问题,我不确定为什么。 If I assign values to xRange and yRange in the GridGenerator class, there doesn't seem to be a problem. 如果我在GridGenerator类中为xRangeyRange GridGenerator ,似乎没有问题。 When I use the constructor in the MapperMain class, I get this error. 当我在MapperMain类中使用构造函数时, MapperMain此错误。

This line 这条线

grid = new int[yRange][xRange];

should be in the constructor, since in your code sample grid has never been initialized with proper values of yRange and xRange . 应该在构造函数中,因为在代码示例grid中,从未使用yRangexRange适当值初始化grid

So - your class should look something like this: 所以-您的课程应该看起来像这样:

public class GridGenerator {
     private int xRange, yRange;
     private int[][] grid;

     //constructor
     public GridGenerator(int xInput, int yInput) {
         xRange = xInput;
         yRange = yInput;
         grid = new int[yRange][xRange]; 
     }

     ...
}

The problem is this line: 问题是这一行:

int[][] grid = new int[yRange][xRange];

despite being coded after the constructor, is executed before the constructor executes, and when the line executes the size variables have their default initialization values of 0 . 尽管在构造函数之后编码,但在构造函数执行之前执行,并且在执行行时,size变量的默认初始化值为0

The reason it executes before the constructor is due to the initialization order: (among other things) All instance variables are initialized, in the order coded, before the constructor executes. 它在构造函数之前执行的原因是由于初始化顺序引起的:(除其他事项外)所有实例变量都在构造函数执行之前按照编码顺序进行了初始化。

To fix the problem, change your code to: 要解决此问题,请将代码更改为:

// remove variables yRange and xRange, unless you need them for some other reason
int[][] grid;

public GridGenerator(int yRange, int xRange) {
    grid = new int[xRange][yRange];
}
int[][] grid = new int[yRange][xRange]; 

您必须在数组范围内给出一些固定值,如果要查找动态数组,请使用数组列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM