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

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

问题描述

我正在开发一个数独解决程序,我需要一个数组列表来保存 9x9 板上每个方块的数字 1 到 9.这些数组列表中的每一个都对应于可以进入该方格的可能数字,如果某个数字不能进入该方格,则将其从列表中删除.

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.

我希望能够提取它正在处理的当前方块的数组列表,例如,如果我想从与方块 (3,5) 对应的数组列表中删除数字 7

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));

但是我不知道如何做到这一点.当我尝试创建数组时,我在声明数组列表数组的行中收到此错误

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

这是我的代码:

    //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;
        }       
    }

}

我这样做是错误的还是不可能创建一个数组列表?如果不可能,那我该怎么做?

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 表示数字的可能性.测试、添加、删除单个数字是 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:数组列表的二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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