java arraylist二维,Java的:的ArrayList的二维数组?

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.

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

Cannot create a generic array of ArrayList

Here is my code:

//create arraylist

ArrayList nums = new ArrayList();

//fill arraylist with numbers 1-9

for (int i = 1; i < 10; i++) {

nums.add(i);

}

//create 9x9 array of arraylists

ArrayList[][] array = new ArrayList[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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值