File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes overriding the array.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding the array...." Watch "overriding the array...." New topic

overriding the array....

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi All,

i have loop, inside the loop i set the value for the array object called
then,i set the value to map

map.put("months"+i, month_with_data);//i is a counter

but outside loop,if i want to get map.get(months0) showing the current iteration value..(it is overriding..i want the key /value fair)..

so ,is there any way to prevent this ?

please explain me...

thanks & regards,
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46427
Difficult to understand your problem.
Are you putting the same object into the Map several times? If so you wil get the same object back, and it will change date as you wait.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Your problem is indeed unclear. But, whatever I get out of it is:

You are filling an array object with some date/month type strings or something. Now, then you are feeding this to a map by map.put("months"+i, month_with_data);//i is a counter . Finally, when you try to retrieve values from map you get the key distinct but, value is always the last index in array.

If it's like so, then the simple thing is that you are doing something wrong while filling your Array.

Also, ("months"+i, month_with_data); is not the correct way of doing things. A map is a key-value pair. Now, what you are doing is you are assosiating an entire array object to a key. Well, what I understand is that you don't want that. What you want is simple key-month instance relationship. So, instead of binding the Array just, bind the month instances directly to the key.
The map wouldn't automatically understand that, it has got a collection and should attach the keys for that automatically.

Well, it would be better if you could make the problem more clear unless it is something different.
wood burning stoves
subject: overriding the array....
jQuery in Action, 3rd edition