Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to search by instance variable's value?

 
Pakawat Gibson LP
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a for-loop to iterate the ArrayList?
[ August 15, 2008: Message edited by: Jelle Klap ]
 
Pakawat Gibson LP
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guy.

So there is no way to access/get that object out without iteration?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give an idea about your object and it's instance member you want to act as a key?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic