Java的:的ArrayList的二维数组? [英] Java: 2D array of arraylists?

查看:1772
本文介绍了Java的:的ArrayList的二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个数独的解决方案,我需要保存数字1到9每个在9x9的板正方形的ArrayList。所有这些的ArrayLists的对应可能的数字,可以去在广场上,如果一个数字不能在广场去,这是从列表中删除。

I am working on a sudoku solving program and I need an arraylist that holds the numbers 1 thru 9 for each of the squares on the 9x9 board. Each of these arraylists correspond to the possible numbers that could go in that square, if a number can not go in that square, it is removed from the list.

我希望能够拉起它正在研究,例如像当前方块的数组列表,如果我想从对应方ArrayList中取出7号(3,5)

I want to be able to pull up the arraylist of the current square it is working on, like for example if I wanted to remove the number 7 from the arraylist corresponding to square (3,5)

arrayOfLists[3][5].remove(Integer.valueOf(7));

不过,我无法弄清楚如何做到这一点。当我尝试创建我就行了获取此错误的数组,其中我宣布我的ArrayList数组

However I can't figure out how to do this. When I try to create the array I am getting this error on the line where I declare my array of arraylists

无法创建ArrayList的一个通用阵列

Cannot create a generic array of ArrayList

下面是我的code:

    //create arraylist
    ArrayList<Integer> nums = new ArrayList<Integer>();

    //fill arraylist with numbers 1-9
    for (int i = 1; i < 10; i++) {
        nums.add(i);
    }

    //create 9x9 array of arraylists
    ArrayList<Integer>[][] array = new ArrayList<Integer>[9][9];

    //fill each element of array with arraylist of numbers 1-9
    for(int i = 0; i<9; i++){
        for(int j = 0; j<9; j++){
            array[i][j] = nums;
        }       
    }

}

我是否正确做这个或者是无法创建的ArrayList数组?如果这是不可能的,我应该怎么做呢?

Am I doing this incorrectly or is it not possible to create an array of arraylists? If it is not possible, how should I do this then?

推荐答案

使用位字段,而不是一个数组列表中。也就是说,使用所有位1-9重新present号码的可能性整数。测试,添加,删除一个数字是O(1),它有一个固定的存储器大小。封装在自己的对象,知道行动的整数。

Use a bit field instead of an array list. That is, use an integer where bits 1-9 represent the possibilities of the numbers. Testing, adding, removing a single number is O(1), and it has a fixed memory size. Encapsulate the integer in its own object that knows the operations.

这篇关于Java的:的ArrayList的二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆