This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding the array...." Watch "overriding the array...." New topic
Author

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
month_with_data
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)....it 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,
seetharaman
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
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.
 
 
subject: overriding the array....
 
Similar Threads
Question about finding common integer from two integer array
can any one explain Two -Dimensional Arrays (code)
array for each loop problem
How to move array item from top of list to bottom of list
array to arraylist