Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:49:40+00:00 2026-05-31T23:49:40+00:00

I have two arrays that I create like this: public int GameBoard[][] = new

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:49:41+00:00Added an answer on May 31, 2026 at 11:49 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I need to create two arrays like this: double[][] TrainPatterns = { new
I have an algorithm where I create two bi-dimensional arrays like this: TYPE TPtrMatrixLine
I have two arrays that I would like to compare and ultimately wind up
I have a class that contains the following two properties: public int Id {
Let's say I have two arrays: int[] array1 = new int[2000000]; int[] array2 =
I have two Arrays of object, one has a set of options like this
I have two arrays of data that I'm trying to amalgamate. One contains actual
Assume that I have two arrays as follow: $array1 = array(1, 3, 5); $array2
Supposed that I have two arrays: Dim RoomName() As String = {(RoomA), (RoomB), (RoomC),
I have following two arrays. I want the difference between these two arrays. That

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.