3

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"

where 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?

2

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.