Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Odd behaviour in map of coordinates

  1. #1
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Odd behaviour in map of coordinates

    Hi,
    I have a HashMap<Character, int[]> full of coordinates of letters in 5x5 table (char[5][5]), which I'm using for a cipher program I'm writing. Basically in order to encode each letter I take the coordinates of the letter and select a letter at different coordinates based on certain rules, but it wasn't working correctly for a reason which took me quite a while to work out. The problem was that I was creating an int array of the original coords, and then editing that array, thinking that it was just a copy and it should have no effect on the map, but it was affecting the map. I've recreated a very simple version of this problem, hopefully someone can tell me why this is happening.

    import java.util.*;
     
    public class Cipher
    {
       public void test()
       {
           Map<Character, int[]> m = new HashMap<>();
            int[] v = new int[2];
            v[0] = 0; v[1] = 0;
            m.put('A', v);
     
            // coord should be a copy of the  map value 'A' right?
            int[] coord = m.get('A');
     
            // prints A : 0,0 as expected
            System.out.println(String.format("A : %d,%d", coord[0], coord[1]));
     
            // editing coord shouldn't affect the value in the map
            coord[0] = 1; coord[1] = 1;
     
            int[] newCoord = m.get('A');
     
            // Map value for 'A' is now 1,1
            System.out.println(String.format("A : %d, %d", newCoord[0], newCoord[1]));
       }
     
       public static void main(String[] args)
       {
          Cipher c = new Cipher();
          c.test();
       }
    }

    This only happens when the value is an array. When i tried this with a Map<Character, Integer>, the map wasn't affected, as expected.

            Map<Character, Integer> m2 = new HashMap<>();
            m2.put('A', 0);
            int c = m2.get('A');
            c = 1;
            // the value of 'A' is still 0
            System.out.println(String.format("A : %d", m2.get('A')));

    What is happening here? Why is the value of the first map being changed? Thanks in advance!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Odd behaviour in map of coordinates

    editing that array, thinking that it was just a copy and it should have no effect on the map, but it was affecting the map
    If more than one part of the program has access to an array and one part changes the contents of the array, all other parts of the program will see the changes. To prevent that, do not return a reference to the array, rather create/clone a new array and return that. That will preserve the contents of the original array.

    Add this print statement at the end of the test method to see what is happening:
            System.out.println("v="+v +", coord=" + coord +", newCoord="+newCoord); //  show hashcode/address
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    dboxall123 (June 9th, 2020)

  4. #3
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Odd behaviour in map of coordinates

    I think i get it. Is this kind of like in C, where if say you send an array to a function, you're actually sending a pointer to the memory location of the first element? And when I assigned the array coord to map.get('A'), instead of copying it like I thought it would do, it assigned the memory location of the map value of 'A'?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Odd behaviour in map of coordinates

    references in java are like pointers in C. The Map's put method copies the reference to the array into its storage. The array is left where it was.
    The Map's get method returns the reference to the array.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Oct 2018
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Odd behaviour in map of coordinates

    OK great, thank for your help

Similar Threads

  1. Unexpected Behaviour of ArrayList
    By MUDIREDDITHILAK in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 17th, 2014, 02:50 AM
  2. netbeans crazy scrolling behaviour
    By startas in forum Java IDEs
    Replies: 7
    Last Post: January 29th, 2014, 09:43 AM
  3. Funny Behaviour
    By lanmonster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 05:04 AM
  4. Quicksort algorithm displaying strange behaviour
    By Mathew.Williams in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 19th, 2011, 12:56 PM
  5. Swing incorrect behaviour from JRE5 to JRE6
    By singhkanhaiya in forum AWT / Java Swing
    Replies: 1
    Last Post: August 25th, 2009, 01:23 AM