-1

I need help because I can not see where my problem is. I have function swapping and it looks like this:

public static void swapping(int[] curr, Integer i, Integer j, Integer k, Integer l, int z, int dist) {
    int tmp;
    int[] tmpCurr = curr;
    if (i >= 0 && j >= 0 && k < 0 && l < 0) {
        tmp = tmpCurr[i];
        tmpCurr[i] = tmpCurr[z];
        tmpCurr[z] = tmp;
        nodesHM.put(1, tmpCurr);
        for(int s = 0; s<tmpCurr.length; s++)
            System.out.println(tmpCurr[s]);

        tmpCurr = curr;
        tmp = tmpCurr[j];
        tmpCurr[j] = tmpCurr[z];
        tmpCurr[z] = tmp;
        nodesHM.put(2, tmpCurr);
        for(int s = 0; s<tmpCurr.length; s++)
            System.out.println(tmpCurr[s]);
    }
}

i,j,k,l tell me what to swap with z. So if i put that somethig is -1 that means I do not want to swap it. Like it this case k and l.

And I call it from main, like this:

public static void main(String[] args) {
    swapping(ourStartState, 3, 7, -1, -1, 6, 5);
}

private static final int[] ourStartState = { 1, 2, 3, 4, 5, 6, 0, 7, 8 };

And it should write me something like: 1,2,3,0,5,6,4,7,8 and 1,2,3,4,5,6,7,0,8

but it give me 1,2,3,0,5,6,4,7,8 and 1,2,3,0,5,6,7,4,8

Where I am going wrong? Thanks

alex
  • 39
  • 3
  • You are going wrong by not using a debugger and finding out exactly what is going wrong – user1231232141214124 Apr 11 '16 at 16:32
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Apr 11 '16 at 17:17

1 Answers1

0

whenever you copy an array or an object, you just create another reference to the elements in the array. so when you did tmpcurr=curr you created another reference to curr array, whatever changes you make in curr affects tmpcurr and vice-versa. if you swap elements in tmpcurr array the elements of curr get swapped as well.

when you swapped elements again you are swapping from already swapped array.

this will help you How do I copy an object in Java?

Community
  • 1
  • 1
Sathkrith
  • 26
  • 3