wood burning stoves*
The moose likes Beginning Java and the fly likes How to search by instance variable's value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to search by instance variable Watch "How to search by instance variable New topic
Author

How to search by instance variable's value?

Pakawat Gibson LP
Greenhorn

Joined: Jul 23, 2008
Posts: 11
I wonder if there is a way to walk through the ArrayList of Object and know the index of the first one which has an expected instance variable's value.

Please help me figure out how to solve this problem.

Anyway, is there structure outside to use instead of ArrayList for this case?

Many thanks


Poor farmer from Thailand ....
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1753
    
    7

Use a for-loop to iterate the ArrayList?
[ August 15, 2008: Message edited by: Jelle Klap ]

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Pakawat Gibson LP
Greenhorn

Joined: Jul 23, 2008
Posts: 11
Thanks guy.

So there is no way to access/get that object out without iteration?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3426
    
  12
Is the value fixed for any given instance of the object ? If so you could use a HashMap with the value as a key. If it was possible for more than one instance to have the same value then you would need to maintain a list of instances mapped to each key, but retrieving the first instance with a given value would simply be a case of retrieving the list from the HashMap and then getting the first entry in the list.

Edit - I just noticed you wanted the index of the entry rather than the entry itself, so I don't think the HashMap idea would work as the order of entry is not maintained.
[ August 15, 2008: Message edited by: Joanne Neal ]

Joanne
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1753
    
    7

Originally posted by Pakawat Gibson LP:
Thanks guy.

So there is no way to access/get that object out without iteration?


If you don't know the index postition, or can't easily determine what the index postition should be (using some kind of hashing algorithm), not really. You might be able to optimize the process using sort and search algorithms, but that's about it.
[ August 15, 2008: Message edited by: Jelle Klap ]
Pakawat Gibson LP
Greenhorn

Joined: Jul 23, 2008
Posts: 11
I'm not the guy on Computer Science for real, just a super newbie who wants to learn programming. So... I might need to seriously study Data Structure around.

That's what I guess
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Can you give an idea about your object and it's instance member you want to act as a key?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to search by instance variable's value?
 
Similar Threads
how long will final variable live ?
Can u explain this
Reference variable
Enviroment Variable
How this bean is remembering the state?