(Java) How do I retrieve an object when given a value that coincides with one of its variables?
-
class Stuff{ //the class Stuff has 3 instance variables String name; int mass; int volume; } public class ActualProgram{ public Stuff{ //This method is of type Stuff ...show more
-
Answer:
Keep list of object in array or list. Iterate through the objects and check value of each object with the user input. Your loop 'll like this: For ( Stuff stuff: arrayorlistOfStuffs){ If(stuff.name=="userinput"){ Print match; Break; } } Above code uses array or list , for each loop and if statements of java.
V3F5Z6UEV62PWMCO53ZBRHB4HQ at Yahoo! Answers Visit the source
Other answers
You can create an array of such objects and then loop through the array. Stuff stuffArr [] = new Stuff [500]; stuffArr elements do NOT contain the names. They have something like a pointer. So, you need to use something like stuffArr [0] = new Stuff (); to actually create the object.
EddieJ
Related Q & A:
- How can I retrieve the object properties values from the database?Best solution by Stack Overflow
- How do I retrieve a URL in Java?Best solution by Stack Overflow
- How can I retrieve a deleted pic/video from my digital camera?Best solution by easeus.com
- How can I retrieve my contacts that I had with a previous yahoo account?Best solution by answers.yahoo.com
- How can I retrieve a conversation?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.