Hello Everyone. I am making a sudoku solver for an assignment, and I ran into a problem of my 2D Integer array not updating.

The input consists of a 9x9 puzzle with 0s for the missing values. I read this input and then place it into the 2D array. during this process I also count the number of 0s. After I create a 1D Integer array with the length being the number of 0s. I then proceed to link the 0s in the 2D array to one of the elements in the 1D array, which I initialize to 1. I then call a recursion which changed the 1D array using a brute force recursion to generate all the possible solutions. When I print the 1D array yout it changes like it is supposed to, but when I print out the 2D array it still has all the 0s filled in with 1s and doesn't change. What could be causing this problem?

Thanks for the help.

here is the portion of the code that links the elements

Integer[][] ans;
Integer[] pS;

int zeroCount = 0;
ans = new Integer[9][9];

for (int i = 0; i < 9; i++){
            for (int j = 0; j < 9; j++){
                ans[j][i] = new Integer(in.readInt());
                System.out.print( ans[j][i] + " ");
                if (ans[j][i] == 0)
                    zeroCount++;
            }
            System.out.println();
        }
        System.out.println("Num 0s = " + zeroCount);

        pS = new Integer[zeroCount];
        for (int i = 0; i < pS.length; i++){
            pS[i] = new Integer(i + 1);
        }


        int ansCount = 0;
        System.out.println("linking answer array to the solution");
        for (int i = 0; i < 9; i++){
            for (int j = 0; j < 9; j++){
                if (ans[j][i] == 0){
                    System.out.println("linking (" + j + ", " + i + ") and " + ansCount);
                    //pS[i] = new Integer(1);
                    ans[j][i] = pS[ansCount];
                    ansCount ++;
                }
            }
        }

The problem you describe doesn't exist in the code you have provided. When I run that code, instead of having 1s where the 0s were in ans, ans has all the values from pS properly copied. The only problem I see is that pS contains numbers which are totally inappropriate for a sudoku puzzle, such as pS.length which is going to be far beyond 9 in most sudoku puzzles.

pS is the array which holds the the numbers for all the missing spots in the array. since more then 9 numbers can be missing from the puzzle, pS.length can be larger than 9. The idea is that the spot where the 0s were in ans point to an element in the pS array, which in turn point to a number between 1 and 9. so by changing the pS array, the numbers in the puzzle should also change, but that is not the case.

Thanks for the help.

so by changing the pS array, the numbers in the puzzle should also change

I'm curious about what you mean by this. It sounds slightly magical, like when the pS array is changed the ans array should magically change to stay up-to-date with the pS array even though you do nothing to actually cause that to happen.

Surely that's not what you mean. The only way to change the content of an array is by changing that array. It could get nightmarishly confusing if changing one array could cause any number of changes to magically happen in other places. If you want to change element (x,y) of ans then do it like ans[x][y] = newValue, and because there is no magic you can feel secure that doing that won't cause anything other than ans to change.

There is that way of doing it, but since a pointer is pointing to an element in the pS array, the portion of the ans array should take the value of what the pS array is pointing to. So if I have one of the 0s point to pS[0], and then pS[0] points to a new Integer (which will change when I go through the solutions), then whenever pS[0] gets updated by setting it to point to a new Integer object, then the portion of the ans array, which points to pS[0] should have the value of whatever pS[0] is pointing to.

Unlike C language where it is easy to have a pointer that points to an element of an array, in Java you can only point to objects. You can have a pointer to an array, but that is as close as you can come to what you are trying to do; there is no way to access the individual elements except by [].

Another problem with what you are attempting is that all of the elements of an array must be of the same type, so even if it were possible to have a pointer to an array element in Java, you wouldn't be able to store those pointers as elements of an Integer[].

So you're saying that the way I have it set up now, instead of the ans array pointing to the location of the pointer in pS which points to the Integer object, it just takes the value of that pointer, so even if I change the pS values the ans array will still be pointing to the original location?

Thanks for the help

That's right

Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.