*
The moose likes Java in General and the fly likes ArrayList of Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList of Objects" Watch "ArrayList of Objects" New topic
Author

ArrayList of Objects

Sree Shan
Greenhorn

Joined: Oct 12, 2000
Posts: 8
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).
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
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
Sree Shan
Greenhorn

Joined: Oct 12, 2000
Posts: 8
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.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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);
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
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).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Shankar Ganesh
Ranch Hand

Joined: Oct 01, 2000
Posts: 34
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.
 
 
subject: ArrayList of Objects
 
Similar Threads
Java ArrayList into Name value pair
Copy existing Array into new ArrayList
Sorting by 3 variables dynamically in arrayList
compiling help
Need help with using arraylist