Results 1 to 8 of 8
  1. #1
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default adding arrays ot HashMap

    Hi, something that dont make sense to me.

    I have a HashMap:
    Java Code:
    HashMap<Integer, int[]> stepsToCheck = new HashMap<Integer, int[]>();
    This one accepts int as key + int Array as value.

    My loop does this:

    Java Code:
    int stepCount = 0;
    
    while(A[0] != B[0] && A[1] != B[1])
            {
                if(A[0] != B[0])
                {
                    A[0] += 1;
                }
                if(A[1] != B[1])
                {
                    A[1] += 1;
                }
                
                //add the step to our hashmap
                stepsToCheck.put(stepCount, A);
                
                stepCount++;
            }
    So we have a loop that iterates a key and adds an Array as value but my problem is that all keys get the same values. Am I doing something wrong in what you see here?

    keys added are [0,1,2] but they all have the same value. Does this make sense? Please explain if you know.

    Since I am adding the same array on 3 places but just updating the array between the put, will the array added in the preceding hashmap positions be updated along with it because its the same array? Can I clone an array somehow to prevent this? I can not manually make alot of different named arrays for this.
    Last edited by Gatsu; 02-01-2013 at 01:25 AM.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: adding arrays ot HashMap

    Yes.
    You are adding the same array to the HashMap each time.
    All you are doing is changing the values of the entries in the array.
    There is nowhere in that code where you create a new 'A' array.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: adding arrays ot HashMap

    I cant make a new array with the same variable name in the while loop

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: adding arrays ot HashMap

    You can make a new array that the variable A points to, though.
    Java Code:
    while (whatever) {
       A = new int[];  // or whatever A is.
    }
    Of course, this is based on almost no idea about what it is you are trying to achieve.

    Your problem lies in using the same array for all your keys.
    How you need to fix this is largely requirements dependent.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: adding arrays ot HashMap

    this just empties the A array?
    Last edited by Gatsu; 02-01-2013 at 04:55 PM.

  6. #6
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: adding arrays ot HashMap

    ok this is my method, and not even making one array for each position in the hashmap works, all become the same like the last one inserted.


    Java Code:
    public void movePiece(int[] oldPos, int[] newPos)
        {
            int[] checkPos = oldPos;
            
            HashMap<Integer, int[]> stepsToCheck = new HashMap<Integer, int[]>();
            
            int stepX = 0;
            int stepY = 0;
            
            //determine which x direction
            if(checkPos[0] < newPos[0])
            {
                stepX = 1;
            }
            else if(oldPos[0] > newPos[0])
            {
                stepX = -1;
            }
            //determine which y direction
            if(checkPos[1] < newPos[1])
            {
                stepY = 1;
            }
            else if(oldPos[1] > newPos[1])
            {
                stepY = -1;
            }
            
            int stepCount = 0; //count steps to be made
            
            //create all arrays for steps
            int[] stepOne = {0,0}, stepTwo = {0,0}, stepThree = {0,0};
            
            //add steps to the HashMap untill we reach our destination.
            while(checkPos[0] != newPos[0] && checkPos[1] != newPos[1])
            {
                //add a step only if this row/column is not the same as where we want to be at
                if(checkPos[0] != newPos[0])
                {
                    checkPos[0] += (stepX);
                }
                if(checkPos[1] != newPos[1])
                {
                    checkPos[1] += (stepY);
                }
                
                //add the step to our hashmap
                if(stepCount == 0)
                {
                    stepOne = checkPos;
                    stepsToCheck.put(stepCount, stepOne);
                }
                else if(stepCount == 1)
                {
                    stepTwo = checkPos;
                    stepsToCheck.put(stepCount, stepTwo);
                }
                else if(stepCount == 2)
                {
                    stepThree = checkPos;
                    stepsToCheck.put(stepCount, stepThree);
                }
                
                stepCount++;
            }
    Last edited by Gatsu; 02-02-2013 at 03:42 PM.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: adding arrays ot HashMap

    You;re going to need to debug it then.
    Stick System.out.println() calls in there printing out various values.
    Because those are three different arrays.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Gatsu is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: adding arrays ot HashMap

    ok I solved this byt changing this

    stepOne = checkPos;

    to this

    stepOne[0] = checkPos[0];
    stepOne[1] = checkPos[1];

    thank you very much

Similar Threads

  1. removing from a hashmap and adding to the end
    By dogeatdogfood in forum New To Java
    Replies: 2
    Last Post: 05-05-2012, 01:34 AM
  2. Help with creating a HashMap from Arrays
    By lannie1980 in forum New To Java
    Replies: 9
    Last Post: 04-24-2012, 10:39 PM
  3. Replies: 0
    Last Post: 02-27-2012, 02:39 AM
  4. Adding Arrays
    By oudaivd1 in forum New To Java
    Replies: 2
    Last Post: 11-16-2010, 03:44 PM
  5. Use Scanner to parse text file, adding to HashMap
    By JordashTalon in forum New To Java
    Replies: 0
    Last Post: 03-04-2009, 11:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •