Results 1 to 4 of 4
  1. #1
    guerlando is offline Member
    Join Date
    Mar 2016
    Rep Power

    Default Map to an element of an array, even if it changes

    Look out at the following code I made:

    Java Code:
    import java.util.HashMap;
    import java.util.Map;
    import java.util.ArrayList;
    public class Testando{
      public static void main (String[] args) {
        ArrayList<String> k = new ArrayList<String>();
        HashMap<Integer, String> a = new HashMap<Integer, String>();
    The output is "test", even though I changed the item at position 0. I think this is expected since I did a.put(0,k.get(0)); that is, I assigned to the map 0 the value from the array at get(0). But how does HashMap works internally? It copies the values or just store references to it? How could I map to a changing thing?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Map to an element of an array, even if it changes

    Add some comments next to each statement describing what the statement is doing. Those comments should explain what is happening.
    If you have problems, post the code with the comments and we'll help you make meaningful comments.

    Note: Single letter variable names makes the code harder to read and understand. Names should relate to the variable's contents.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Map to an element of an array, even if it changes

    Your array contains a single string "test" at location 0.
    you copy that string into the hash map using 0 as the key.
    You changed the array to a string "changed". This has no effect on what the hash map contains.
    So the hash map contains the string "test" and the array has the string "changed"

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Map to an element of an array, even if it changes

    ... and if the subject line correctly states what you need to do, use a Map<Integer, Integer> where the mapped value is the index to the ArrayList<String>.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  3. Replies: 1
    Last Post: 03-31-2009, 07:40 AM
  4. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 09:44 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 06:26 PM

Tags for this Thread

Posting Permissions

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