0

I've written a function which. Problem is, the parameters I'm sending, is being manipulated in the main program, though it is not my intention. I just want to have the value inside the function, but while operating, the actual value in the main program is also being changed.

How can I prevent this?

Here is my code:

    Tiles[][] MoveRight(Tiles[][] tilesArray) {


    Tiles[][] tempTilesArray = new Tiles[3][3];
    Tiles[][] tempTilesArrayToSend = new Tiles[3][3];

    tempTilesArrayToSend = CopyTilesArrays(tilesArray, tempTilesArrayToSend);

    ArrayIndex zeroPos = FindZero(tilesArray);

    Tiles zeroTile = GetTile(zeroPos, tilesArray);

    if (zeroPos.column != 2) {
        ArrayIndex otherPos = new ArrayIndex(zeroPos.row,
                zeroPos.column + 1);
        tempTilesArray = SwapTilesPositions(zeroTile, GetTile(otherPos,
                tilesArray), tempTilesArrayToSend);
    }
    return tempTilesArray;

}

The array I'm sending inside the SwapPositionFunction is actually modifying the tilesArray itself. Though I've made a new instance of tiles array and then sent it.

3
  • Did you mean "inside the SwapTilesPositions() function"? Commented Jan 31, 2012 at 20:57
  • 1
    You should post the CopyTileArrays() function. It would have to be returning its first argument, and discarding its second. Commented Jan 31, 2012 at 20:59
  • Maybe the CopyTilesArrays function makes a shallow copy? Commented Jan 31, 2012 at 20:59

1 Answer 1

1

Without seeing what is done in

CopyTilesArrays (tilesArray, tempTilesArrayToSend);

we can not say much.

Note, that in Java, there is no pass-by-value or pass-by-reference, but a copy of the reference is passed to the methods. This copy of a reference will - in case of objects and Arrays - point to the same, original object, so if you change the underlying/embedded object, the original object is affected, but if you change the reference, the original object is not affected.

IF you want to pass an independent copy of your array, you have to perform a deep ocpy. Maybe that is, what CopyTilesArrays is supposed to do, but without seeing it, we don't know.

Note too, that there are, or better: that there can be several layers of objects, with different reasons to stay on the surface, to go to the core, or to stay somewhere in between.

For example, to make a deep copy from the Array of Array of Tiles, you could do something like this:

public class TilesCopy {
    Tiles[][] copyTilesArrays (Tiles[][] from, int outer, int inner) {
        Tiles[][] to = new Tiles[outer][inner];
        int o = 0;
        for (Tiles [] tiles: from) {
            Tiles[] fresh = new Tiles [inner];
            int i = 0;
            for (Tiles t : tiles) 
            {
                fresh[i] = t.deepCopy ();
                i++;
            }
            to [o] = fresh; 
            o++;
        }
        return to; 
    }
}

Note, that in the innermost loop, the elements aren't just referenced with fresh[i] = t;, but with a deep copy, to keep the objects in the original Array unaffected.

You could copy an array of arrays of Tiles in multiple other ways. For example, you could rearrange the outer array. If the Tiles were

[[A][B][C]]
[[D][E][F]]
[[G][H][I]]

you could copy them, and modify the target to be:

[[G][H][I]]
[[D][E][F]]
[[A][B][C]]

with just copying the outer arrays, and rearranging them. And you could copy the inner arrays, to be:

[[C][B][A]]
[[F][E][D]]
[[I][H][G]]

If you now modify the A to a, the original A will be affected too, without a deep copy:

[[C][B][a]]
[[F][E][D]]
[[I][H][G]]

[[a][B][C]]
[[D][E][F]]
[[G][H][I]]
Sign up to request clarification or add additional context in comments.

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.