1

I have two arrays that I create like this:

public int GameBoard[][] = new int[30][14];
public int DirectionMap[][] = new int[30][14];

I then initialize the arrays like this:

for (int i = 0; i < GameBoard.length; i++)
{
    for (int j = 0; j < GameBoard[i].length; j++)
    {
        GameBoard[i][j] = 0;
    }
}

... //Same for DirectionMap

When I run the function:

DirectionMap = AStar(GameBoard);

To render the pathfinding map that my units will follow, DirectionMap is correctly set to the values generated based on my GameBoard. However GameBoard is set to the result as well. When I run the application in Debug Mode within Eclipse, I can see that the ID's of the two arrays are the same. For some reason they seem to be pointing to the memory space. My AStar function does not modify the GameBoard array at all. The only reference to it is int retVal[][] = GameBoard;

My function prototype is public int[][] AStar(int[][] Board); and it returns the int[][] retVal.

I have no idea why I cannot change the values of DirectionMap without GameBoard following. I have never had any issues like this before.

Any ideas are really appreciated. Thanks for your help.

        public int[][] AStar(int[][] Board)
        {
        int retVal[][] = Board;

        //Initialize All Needed Lists
        int width = retVal.length;
        int height = retVal[0].length;
        int goalX = 0;
        int goalY = 0;
        //List<Node> fieldInfo = new ArrayList<Node>(); 

        Node fieldArray[][] = new Node[width][height];

        for (int i = 0; i < fieldArray.length; i++)
        {
            for (int j = 0; j < fieldArray[i].length; j++)
            {
                fieldArray[i][j] = new Node(i, j);
                if (retVal[i][j] == 2)
                {
                    fieldArray[i][j].setOpen(1);
                    fieldArray[i][j].setDirection(10); //Set as target
                    goalX = i;
                    goalY = j;
                }
                if (retVal[i][j] == 1)
                {
                    fieldArray[i][j].setOpen(0);
                    fieldArray[i][j].setDirection(9); //Set as wall
                }
            }
        }

                    //Add AStar Algorithm Here

        for (int i = 0; i < fieldArray.length; i++)
        {
            for (int j = 0; j < fieldArray[i].length; j++)
            {
                if (fieldArray[i][j].getDirection() == 0)
                {
                    //Occurs when node was never reached
                    int dX = i - goalX;
                    int dY = j - goalY;
                    if (dX < 0)
                        dX = -dX;
                    if (dY < 0)
                        dY = -dY;
                    if (dY > dX)
                        fieldArray[i][j].setDirection(1);
                    else
                    {
                        if (i > goalX)
                            fieldArray[i][j].setDirection(7);
                        if (i < goalX)
                            fieldArray[i][j].setDirection(3);
                    }
                }
            }
        }

        for (int i = 0; i < fieldArray.length; i++)
        {
            for (int j = 0; j < fieldArray[i].length; j++)
            {
                retVal[i][j] = fieldArray[i][j].getDirection();
            }
        }

        return retVal;
    }
Nathan Tornquist
  • 6,468
  • 10
  • 47
  • 72
  • 1
    Can you post the AStar(int[][]) method code? – matt freake Mar 24 '12 at 18:49
  • I've edited the initial posting. – Nathan Tornquist Mar 24 '12 at 18:53
  • `int retVal[][] = Board;` then later `retVal[i][j] = fieldArray[i][j].getDirection();`. That'll do it. – Squonk Mar 24 '12 at 18:59
  • Should I be using int retVal[][] = new int[val1][val2]; ---- retVal = Board; --- Can I set them equal, or must I run through every value? – Nathan Tornquist Mar 24 '12 at 19:00
  • @MisterSquonk: You answered first. If you post something like what you said below, I'll accept is at the solution. – Nathan Tornquist Mar 24 '12 at 19:06
  • @Nathan: I had to run an urgent errand so didn't have time for a full answer but thought I'd post a quick comment. tsatiz has pretty much covered it - as I expect you've realised now that you are simply copying a reference when using `int retVal[][] = Board;` rather than making a new copy. As tsatiz mentions, doing a full value copy would work although I'd personally just clone `Board` using `int retVal[][] = Board.clone();` - I think that should work. – Squonk Mar 24 '12 at 19:40
  • Thanks for your help. Everything works as expected now. – Nathan Tornquist Mar 24 '12 at 19:45

3 Answers3

2

Remember when you are passing an object to methods you are actually passing a copy of reference. So when you initialise retVal[][] = Board; you actually point Board using another reference retVal. And you are returning the same reference to DirectionMap. Hence same id's for Board and DirectionMap. Consider array copy instead.

Community
  • 1
  • 1
tsatiz
  • 1,081
  • 7
  • 18
0

I can see that the ID's of the two arrays are the same. For some reason they seem to be pointing to the memory space

The int[] array in Java has 0's as default values.

int[] x = new int[2];
System.out.println(x[0]); // prints 0
System.out.println(x[1]); // prints 0
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

The only reference to it is int retVal[][] = GameBoard;

Java arrays are objects. If at any point you are setting array = array, you are setting one reference to point to the same object as the other.