1

I have an array of Booleans like this:

boolean[][] x = 
    {{true, true, false, true, true, true, false, true},
    {false, true, false, false, true, false, false, false},
    {true, false, true, true, true, false, true, true},
    {true, false, false, false, false, true, true, false},
    {false, true, false, true, true, false, true, false},
    {false, true, false, true, false, true, false, false},
    {true, true, false, false, false, false, false, false},
    {true, false, false, false, false, false, false, false},
    {false, true, true, false, true, true, true, true}};

What I want to do is make a HashMap connecting each point in the array to a HashSet containing the set of all points it is adjacent to that are also true. For example, x[1][0] should lead to an empty set because it is false. However, x[2][2] would contain a set containing {2, 3} because both {2, 2} and {2, 3} are true.

Here is my code so far.

HashMap<int[], HashSet<int[]>> graph = new HashMap<int[], HashSet<int[]>>();

int[] key = new int[2];
for (int i = 0; i < x.length; i++){
    for (int j = 0; j < x[0].length; j++) {
        key[0] = i;
        key[1] = j;
        int[] key2 = new int[2];
        HashSet<int[]> addSet = new HashSet<int[]>();
        if (x[i][j]) {
            if (i > 0) {
                key2[0] = i-1;
                key2[1] = j;
                if (x[i-1][j]) addSet.add(key2);
            }
            if (i < x.length - 1) {
                key2[0] = i+1;
                key2[1] = j;
                if (x[i+1][j]) addSet.add(key2);
            }
            if (j < x[0].length - 1) {
                key2[0] = i;
                key2[1] = j+1;
                if (x[i][j+1]) addSet.add(key2);
            }
            if (j > 0) {
                key2[0] = i;
                key2[1] = j-1;
                if (x[i][j-1]) addSet.add(key2);
            }
        }
        graph.put(key, addSet);
    }
}

However, when I ran the code, graph only contained the point {8, 9}. Can you find what the flaw in my code is?

Noooo
  • 61
  • 9
  • 4
    Did you try debugging? – shmosel May 19 '17 at 23:54
  • Yes. On every iteration, the whole graph changes instead of just a part of it. One time it contained {0,0}, and then on the next iteration, that element seemed to be replaced by {0, 2}. – Noooo May 19 '17 at 23:58
  • @Noooo Are you trying to do a `floodfill` like then? – loretoparisi May 19 '17 at 23:59
  • 1
    You use the same `int[] key2` array over and over again. If oyu push the array into the set, the set will not copy, but only reference the array. Thus, your set will always be of size 1 and if you change a value in `key`, you change the only entry in your set. `{8, 9}` Should be the last entry found in the run of your algorithm. – Turing85 May 20 '17 at 00:00
  • 2
    You can't use `int[]` as a hash key because it doesn't implement value equality. – shmosel May 20 '17 at 00:01
  • 1
    @TimBiegeleisen There is also only 1 `key`, which only has 1 identity. One key means one value in the map. – Jorn Vernee May 20 '17 at 00:03
  • I see. So do I need to re-initialize `key` every loop? – Noooo May 20 '17 at 00:05
  • @Noooo `key` after every `graph.put(...)` and `key2` after every `addSet.add(...)` – Turing85 May 20 '17 at 00:06
  • Move `int[] key = new int[2];` _inside_ the two for loops (if I understand correctly). – Tim Biegeleisen May 20 '17 at 00:06
  • And as was mentioned by @shmosel [you should not use arrays as keys for `HashMap`](http://stackoverflow.com/questions/15576009/how-to-make-hashmap-work-with-arrays-as-key). – Turing85 May 20 '17 at 00:09
  • @Turing85 Thanks for the help, I have implemented that and it now works :) – Noooo May 20 '17 at 00:09
  • Should I now change the int[] to List ? – Noooo May 20 '17 at 00:10
  • 1
    You should try to understand what is going on and then attempt to solve the problem on your own. – Turing85 May 20 '17 at 00:10

0 Answers0