我在一个类中有一个实例变量,看起来像
private GridLocation [][] world;
我不确定如何向这样的一对尖括号添加值。我只知道这两个值代表网格中正方形的坐标。例如,您将如何添加平方 (1,2)?根据我的书,您可以在构造函数内赋值
this.name = name;
我非常不确定在这种情况下该怎么办
最佳答案
因为@GBlodgett 已经回答了这个问题。它是一个“多维”数组或数组的数组。它的工作方式类似于单个数组,您可以在不同的数组之间进行选择。
GridLocation [][] world = new GridLocation[x][y];//x, and y are the size of your array
world[1][2] = yourGridLocation;
您还应该看看如何初始化它。 Here是类似问题的链接。
In Java, a two-dimensional array is represented by an array of arrays in which every sub-array is of the same length: quote
关于java - 如何附加到数组实例变量的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52346824/