ArrayList add() replacing the entire list

Put your problem here if it does not fit any of the other categories.

ArrayList add() replacing the entire list

Postby eee333 » Wed Apr 02, 2008 9:03 pm

Very strange. Hopefully I'm just doing something dumb.

I grab an xml file and parse out my data from it.
I add the data to the "item". Works
I add the "item" to the "list", and it replaces all of the elements in the list simultaneously. I want it to only add the new item and leave the others unchanged.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
  3.  
  4. HashMap<String,String> item = new HashMap<String,String>();
  5.  
  6. ...
  7.  
  8. // Loop over the xml and build out the lists
  9.  
  10.                         for ( int i=0; i<nodelistlength; i++ ) {
  11.  
  12.                                 Log.v("forloop", Integer.toString(i));
  13.  
  14.                                 Element noteEL = (Element) myNL.item(i);
  15.  
  16.                                 String bodytext = getTextValue(noteEL,"body");
  17.  
  18.                                 String createdtext = getTextValue(noteEL,"created");
  19.  
  20.                                 Log.v("bodytext", bodytext);
  21.  
  22.                                 Log.v("createdtext", createdtext);
  23.  
  24.                                 item.clear();
  25.  
  26.                                 item.put("line1", getTextValue(noteEL,"body"));
  27.  
  28.                                 item.put("line2", getTextValue(noteEL,"created"));
  29.  
  30.                                 Log.v("Dumping item", item.toString());
  31.  
  32.                                 Log.v("Dumping list", list.toString());
  33.  
  34.                                 list.add(item) ;
  35.  
  36.                             Log.v(bodytext, createdtext);
  37.  
  38.                         }
  39.  
  40.  
  41.  
  42.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


easy enough... but look at the log output

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. VERBOSE/nodelistlength(1100): 8
  3.  
  4. VERBOSE/forloop(1100): 0
  5.  
  6. VERBOSE/bodytext(1100): testing
  7.  
  8. VERBOSE/createdtext(1100): 2008-04-01 16:16:23
  9.  
  10. VERBOSE/Dumping item(1100): {line1=testing, line2=2008-04-01 16:16:23}
  11.  
  12. VERBOSE/Dumping list(1100): []
  13.  
  14. VERBOSE/forloop(1100): 1
  15.  
  16. VERBOSE/bodytext(1100): testing
  17.  
  18. VERBOSE/createdtext(1100): 2008-04-01 16:16:46
  19.  
  20. VERBOSE/Dumping item(1100): {line1=testing, line2=2008-04-01 16:16:46}
  21.  
  22. VERBOSE/Dumping list(1100): [{line1=testing, line2=2008-04-01 16:16:46}]
  23.  
  24. VERBOSE/forloop(1100): 2
  25.  
  26. VERBOSE/bodytext(1100): testing
  27.  
  28. VERBOSE/createdtext(1100): 2008-04-01 16:38:16
  29.  
  30. VERBOSE/Dumping item(1100): {line1=testing, line2=2008-04-01 16:38:16}
  31.  
  32. VERBOSE/Dumping list(1100): [{line1=testing, line2=2008-04-01 16:38:16}, {line1=testing, line2=2008-04-01 16:38:16}]
  33.  
  34. VERBOSE/testing(1100): 2008-04-01 16:38:16
  35.  
  36. VERBOSE/forloop(1100): 3
  37.  
  38. VERBOSE/bodytext(1100): testing
  39.  
  40. VERBOSE/createdtext(1100): 2008-04-01 17:09:50
  41.  
  42. VERBOSE/Dumping item(1100): {line1=testing, line2=2008-04-01 17:09:50}
  43.  
  44. VERBOSE/Dumping list(1100): [{line1=testing, line2=2008-04-01 17:09:50}, {line1=testing, line2=2008-04-01 17:09:50}, {line1=testing, line2=2008-04-01 17:09:50}]
  45.  
  46. ...  
  47.  
  48. etc
  49.  
  50.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


See how the time stamp is the same for each entry in the list? Why?

Any clues?

Thanks for the help! anddev rocks.
eee333
Freshman
Freshman
 
Posts: 9
Joined: Tue Mar 25, 2008 10:35 pm

Top

Postby cadlg » Wed Apr 02, 2008 9:36 pm

Have you tried creating the item in each loop iteration ? (i.e. placing "HashMap<String,String> item = new HashMap<String,String>(); " inside the for loop)

I'm not sure if this is going to make any sense, but it seems that the add method of the list is just adding a pointer to the element, and not making a copy of it, so everytime you assign something to the element, you overwrite what you assigned in the previous iteration, and since the add (assumed) is adding only a pointer, you have in the list, many pointers to the same element, whose values are changing every loop iteration...

What do you think ?

Regards,

cadlg
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

YES

Postby eee333 » Wed Apr 02, 2008 10:02 pm

Awesome!

That did it.

:D :D :D :D :D

Thanks
eee333
Freshman
Freshman
 
Posts: 9
Joined: Tue Mar 25, 2008 10:35 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests