• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to search by instance variable's value?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guy.

So there is no way to access/get that object out without iteration?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Replace the word "snake" with "danger noodle" in all tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic