This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Need Suggestion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Suggestion" Watch "Need Suggestion" New topic
Author

Need Suggestion

Harathi Rao
Ranch Hand

Joined: Oct 31, 2004
Posts: 42
Hi

I have a typical situation where i have setter and getter methods like this
Item item = new Item();
item.setValue1()
Item.getValue1()
.
.
.
.
item.setValue6()
item.getValue6()

And I got a List data = {"hello","good","morning"};

I need to set these values and get them using above setter and getters..
item.setValue1(data.get(0));
item.setValue2(data.get(1));

Now, is there a simple way to do this... by using for loop or any such logic...


Thanks in anticipation!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20077
    
  30

If those are really your method names, why not make them indexed?

So instead of the 6 setValue methods and 6 getValue methods, have one of each:

Of course, if you store your values in an array as well, you can just use the array index; it will throw an IndexOutOfBoundsException of its own.
Do remember though that in Java, arrays are 0 based. So you might want to use "array[index - 1]" to keep your methods 1 based.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Srikanth Basa
Ranch Hand

Joined: Jun 06, 2005
Posts: 241
You can plan to use Java reflections, if you are allowed to.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Need Suggestion
 
It's not a secret anymore!