• 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

polyNOOBisim

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy pardners,

Can someone please tell me how to reference objects through the type or value of their variables, rather than the actual reference.

This is a program idea I had that prompted me to start learning, so I don't have any code yet, but essentially I want to say this in Javanese:

PERFORM {operation} ON (object with variable of type X)
or/and
PERFORM {operation} ON (object with X as the value for any variable).

My thinking at the moment is to put everything in an array then loop through it and use exceptions. Or design the classes in some fiendish manner which currently eludes me.

Please tell me the easy answer or point me to where I can find it.

Thanks, you guys have helped me a lot already.

Scott
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Scott,

Welcome to JavaRanch!

Your ideas are basically correct. If the objects are all of the same type, then instead of using an array, I might use multiple Maps (like java.util.HashMap). Looking things up in a Map is both simpler and faster than searching linearly through an array. You could use one map for each variable, and use the value of the variable as the key for an object.

If you want to do this for many different kinds of objects, then the best thing to do (conceptually) would be to put the objects into a database, with a table like



Then you can find the objects of interest using a database query. There are lots of different databases to choose from, including library-based in-memory ones and free software ones.

Just thinking out loud; hope this helps.
 
scott beveridge
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ernest,

And yes it does help very much. Next on the learning curve is hashmaps. The objects all have the same base class so I may adjust things to make it easier. Its just a matter of working out exactly what has to happen.

I was intending to use a database for something else, but after re-reading your post a few times you gave me an idea.
Can multiple maps be used to store a reference to code that will be chosen from at run-time? Like maybe handed to a new thread while the main one is halted to wait for the return? Even if the reference was just to a file location containing the code.
With something like that I could avoid using (learning about) a database entirely and store everything in a seperate file.

Then again the size could run to a few hundred entries, is that an acceptable overhead for the map?

My turn to think out loud.

Scott
 
reply
    Bookmark Topic Watch Topic
  • New Topic