public class Environment {
//variables
RoundingStage[][] land;
int horizontalStreets;
int verticalStreets;
public Environment(RoundingStage[][] grid) {
this.horizontalStreets = grid.length;
this.verticalStreets = grid[0].length;
land = new RoundingStage[horizontalStreets][verticalStreets];
for (int i = 0 ; i <= horizontalStreets ; i++)
for (int j = 0 ; j <= verticalStreets ; j++)
land[i][j] = grid[i][j];
}
public Environment(Environment env) {
this.horizontalStreets = env.horizontalStreets;
this.verticalStreets = env.verticalStreets;
land = new RoundingStage[horizontalStreets][verticalStreets];
for (int i = 0 ; i <= horizontalStreets ; i++)
for (int j=0 ; j <= verticalStreets ; j++)
land[i][j] = env.land[i][j];
}
有人可以告诉我为什么我使用此功能时会得到ArrayOutOfBound吗?我已经尝试过+ 1,-1的每种组合,请给我一些帮助:D
public RoundingStage valueInPosition(int rHor, int rVer) {
return land[rHor-1][rVer-1];
}
最佳答案
实际上,您应该在那些构造函数中摆脱限制。那些使用i
和j
作为迭代器的嵌套循环应该一直循环到i < horizontalStreets
和j <verticalStreets
,而不是当前发布的<=
。 Java数组索引基于0。