0

My app calculates distances between cities. At some time, i get an ArrayList hash map of the form:

 [{username=p, startcity=A, finalcity=B}, 
{username=p, startcity=C, finalcity=D}, 
{username=f, , startcity=E, finalcity=D}, 
{username=e, startcity=F, finalcity=L}]

Few lines below, i get ArrayList

[{username=p, startcity=A, finalcity=B},
{username=e, startcity=F, finalcity=L}, 
    {username=p, startcity=C, finalcity=D}, 
    {username=f, , startcity=E, finalcity=D}, 
    {username=e, startcity=F, finalcity=L}]

Note that objects at positions 1 and 4 are exactly the same. Then i try to assign at every object a different number. Problem is that the aforementioned objects are getting every time same value...

Code...

for(n=0;n<5;n++){
ArrayList.get(n).put("n",String.valueof(n));
}
Log.d("ArrayList", ArrayList.toString.....

leads to...

[{username=p, startcity=A, finalcity=B , n=0},
{username=e, startcity=F, finalcity=L,n=4!!!!//here...}, 
    {username=p, startcity=C, finalcity=D,n=2}, 
    {username=f, , startcity=E, finalcity=D,n=3}, 
    {username=e, startcity=F, finalcity=L,n=4}]

So i can't differentiate those two objects. Why; Isn't that weird; Is there a way to do this; Thanks in advance

2
  • Can you add your entire code here Commented Jan 30, 2015 at 11:52
  • The code above was a very simple translation of the real problem. Code is to big to post here. But..can it happen regarding to the above simple example; Or i'm dreaming... Commented Jan 30, 2015 at 11:59

2 Answers 2

0

Firstly, I don't understand what ArrayList hash map means. ArrayList and HashMap is entirely different. Did you do some transformation?Maybe Something wrong happens when you transform the ArrayList to HashMap.

Secondly, you said "few lines below", I think you should paste that few lines, it's necessary for others to help solve your problems.

I hope you can edit your problem again. More details is necessary for solving this problem.

Sign up to request clarification or add additional context in comments.

3 Comments

Eh...1.how do you print the first ArrayList. 2. why is there the item with n = 5 when n<5 in your for loop. I don't think your code can lead to that print.
sorry...as i said, this was an example. Problem remains
0
ArrayList.get(n)

will get the same object when the value of n will be 4 and will override the same object which is placed when n is 1.

1 Comment

Any suggestion to differentiate those two objects;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.