[英]Create 2D array of Lists in Java
I'm trying to create a 2D array of lists for a Sudoku. 我正在尝试为Sudoku创建2D列表列表。 Essentially 81 lists each containing possible solutions to that box in the Sudoku grid. 本质上,81在Sudoku网格中列出了每个包含该框可能解决方案的列表。 I've tried multiple declarations so far, but whenever I try to add values to a list it returns a null pointer exception. 到目前为止,我已经尝试了多个声明,但是每当我尝试向列表中添加值时,它都会返回空指针异常。 Here is an example, simply populating each of the lists with the numbers 1-9. 这是一个示例,仅用数字1-9填充每个列表。
List<Integer>[][] sudoku = (List<Integer>[][]) new List[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 1; k < 10; ) {
sudoku[i][j].add(k);
}
}
}
I'm not even positive a 2D array of lists is the optimal way to go about this, but I've done everything from scratch (with a relatively low knowledge of java) so far so I'd like to follow through with this method. 我什至不能肯定列表的2D数组是实现此目的的最佳方法,但是到目前为止,我已经从头开始(对Java的了解相对较少),因此我想继续使用此方法。 The original code looked as follows: 原始代码如下所示:
List[][] sudoku = new List[9][9];
Research quickly revealed that this wouldn't cut it. 研究迅速表明,这不会削减成本。
Thank you in advanced for any help! 在此先感谢您的帮助!
Try this one. 试试这个。 The general idea, create a master list and while you loop through it, create one inner list. 通常的想法是创建一个主列表,并在遍历该列表时创建一个内部列表。
/* Declare your intended size. */
int mainGridSize = 81;
int innerGridSize = 9;
/* Your master grid. */
List<List<Integer>> mainList = new ArrayList<List<Integer>>(mainGridSize);
/* Your inner grid */
List<Integer> innerList = null;
/* Loop around the mastergrid */
for (int i=0; i<mainGridSize; i++) {
/* create one inner grid for each iteration of the main grid */
innerList = new ArrayList<Integer>(innerGridSize);
/* populate your inner grid */
for (int j=0; j<innerGridSize; j++)
innerList.add(j);
/* add it to your main list */
mainList.add(innerList);
}
Illustrated: 说明:
If you need to vary your grid, just change the values of the gridSize. 如果需要更改网格,只需更改gridSize的值即可。
You cannot create array of generic lists . 您不能创建通用列表数组 。
You can create List of Lists: 您可以创建列表列表:
List<List<List<Integer>>> soduko = new ArrayList<>();
And then populate it as you wish. 然后根据需要填充它。
or use casting: 或使用强制转换:
List[][] soduko = (List<IntegerNode>[][]) new LinkedList[9][9];
You have create the array of Lists
but you did not initialize it. 您已经创建了Lists
数组,但尚未初始化它。 Insert this in the second line an the problem should be solved. 将其插入第二行,应解决问题。
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++){
sudoku[i][j]=new ArrayList<Integer>();
}
}
Or to do it all in one go do it like this: 或一次完成所有操作,如下所示:
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sudoku[i][j]= new ArrayList<Integer>();
for (int k = 1; k < 10; ) {
sudoku[i][j].add(k);
}
}
}
If you know that you need exactly 81 2D arrays, you can create 3D array: 如果知道确切需要81个2D阵列,则可以创建3D阵列:
int[][][] sudoku = new int[81][9][9];
The way you did it now would produce compilation error. 您现在执行的方式将产生编译错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.