I have string of array with size already defined and i have to iterate though this array and fill it with fields value of someObject I have . If i do this with if elseif elseif or switch case i get high complexity .. please kindly suggest proper way to do this
Asked
Active
Viewed 680 times
0
-
Why have you included your code as an image? Also why are you using a for loop here? – George Simms Aug 04 '15 at 06:03
-
It would be better if you post your code as text instead of image – Naman Gala Aug 04 '15 at 06:03
-
Why don't you hard code numbers in place of i like `value[0] = bean,getEmployeeI();` (do it for other values also) and remove for loop. – Naman Gala Aug 04 '15 at 06:05
-
i tried , I am not able to post the code as error prevents me to post so some how i find way to post my question , – user1659644 Aug 04 '15 at 06:05
-
When you edit your question there is `?` icon, you can click on it. You will examples. – Naman Gala Aug 04 '15 at 06:08
-
Add a `toArray()` to your 'bean'? – Aug 04 '15 at 06:12
1 Answers
0
I would suggest you to use a map to keep the mapping like this:
private static final Map<Integer, String> MAP_VALUES = new HashMap<Integer, String>();
private static Map<Integer, String> getMapValues(Employe employe){
if(MAP_VALUES.isEmpty()){
MAP_VALUES.put(0, employe.getEmployeeI());
MAP_VALUES.put(1, employe.getEmployeeLastName());
MAP_VALUES.put(2, employe.getEmployeeDivision());
....
}
return MAP_VALUES;
}
public String getValueForEmploye(Employe employe){
String[] value = ...;
for(int i = 0; i < value.length; i++){
value[i] = getMapValues(employe).get(i);
}
}

bryce
- 842
- 11
- 35
-
@namangala's comment is the right solution. There is no reason to use a loop, and the Map mechanism is harmful overhead.The PMD complexity is a misleading indicator as it didn't spot that this construct added inefficiency. – Aug 04 '15 at 08:38