If I have a arraylist of object, how can I retreive a particular object using the object name? ie, If I have an arraylist of Cities, how can retrieve a particular City object (within this array list) using the CityName (ie, name of the City object).
Since YOU control the ordering of the arrayList, you would need to walk through all of the elements and look for it. Unless of course you have some clever strategy for putting the objects in order. Then you would need to use that strategy to retrieve them. If you are looking for an indexed list perhaps a sortedMap would be good.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Sree, If you want to really use an ArrayList, then I would suggest the following.
In the end, I would go with Cindy's suggestion and use a SortedMap. Hope this helps. -Peter
Joined: Oct 12, 2000
Thanks Cindy for the response. The ArrayList would be populated in a particular order and it needs to be retained - so SortedMap maynot help.. Correct me if I am wrong. But my main question is how to retreive the object when all I know is the ClassName (ie, the string)... Would Class.forName() be helpful in this case - Would Class.forName() instantiate a new instance or can it pick up the existing object itself... Can you please clarify.
Joined: Sep 29, 2000
All that class.forName() would do is tell you that each object in the ArrayList is a City. Not what you intended I think. If you are just looking for a particular city in the list Peter showed you a binarySearch above. Or you could you use indexOf City findMe = new City("Dallas"); int foundItsIndex = list.indexOf(findMe);
Joined: Jan 02, 2001
Cindy, I did try using indexOf(findMe), but it didn't work. I got a -1 returned from the method. I haven't study the implementation, but it looks like if you create a new City it's looking for the same object reference and not the content of the City Object. So in my example, I added a which is a different City object than When I modified the code to add Dallas to the list from a local City object, and used this same object for the search using indexOf() then it returned true.
I didn't want to use a binary serach, because it makes the code more complicated. This is the only way I know how using a List. -Peter [This message has been edited by Peter Tran (edited January 05, 2001).]
In order for indexOf() to work the way you want it to, your City class will need to implement equals() to determine if two different City objects are really equivalent. Otherwise indexOf() uses the Object equals() method, which just compares references (like ==). However, assuming that the City class stores more than just the city name, and that you nonetheless want to be able to find a city using its name only, then indexOf() probably isn't what you want. I'd suggest that you need a method that will find a city using its name, and that method can either (a) walk through the whole ArrayList searching for the name, or (b) make use of a private Map which is separate from the ArrayList. This Map would have key: String for city name, and value: Integer representing index of city in ArrayList. Use the Map to get the index, then use the index to get the City object from the ArrayList. Or the Map could map directly to the City object, bypassing the ArrayList and its index. You could then use indexOf() to recover the index in the ArrayList if you needed it.
Yes... For the specified requirement, Map or a specific method within the ArrayList is required. I think instead of writing a specific method, a Map can be used if that would provide the required functionality.