开发者

Creating an array(?) of Sets in Java

I'm trying to write an algorithm that finds the number of solutions in a given partially filled in Sudoku board. i.e. given

"1 6 4 0 0 0 0 0 2",
"2 0 0 4 0 3 9 1 0",
"0 0 5 0 8 0 4 0 7",
"0 9 0 0 0 6 5 0 0",
"5 0 0 1 0 2 0 0 8",
"0 0 8 9 0 0 0 3 0",
"8 0 9 0 4 0 2 0 0",
"0 7 3 5 0 9 0 0 1",
"4 0 0 0 0 0 6 7 9"

wh开发者_运维知识库ere 0s represent blank spots. I want to create 3 separate arrays of sets, one for each set of numbers in each column, row, and 3x3 square. I am trying the declaration:

horizontal = new HashSet<Integer>[9];

Where private HashSet[] horizontal is declared earlier, but this doesn't work. What is the correct declaration or can I not make declare an array of Sets?


The problem is the type parameter. You can't create generic arrays in Java. You can remove the type parameter and it will work, but you should get a warning about unchecked operations.


You might try this:

horizontal = new ArrayList<HashMap<Integer>>();

Access it with horizontal.get(1); and you can treat it the same way as a normal array. As @user599152 said, you can't create generic arrays. So you need to figure out another way. A list is probably your best bet.


An array of sets is a strange way to store the data. A 2d array or a 2d array of 2d arrays might be a much more intuitive way of modeling this. Possibly even a 2d array of some custom class ("SudokuCube") would also work better than an array of sets.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜