| 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: 19214
|
|
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
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.
|
 |
 |
|
|
subject: Need Suggestion
|
|
|