Once upon a time, there were the following fields in one place.
public class Xxx {
public String xxx1;
public String xxx2;
public String xxx3;
public String xxx4;
public String xxx5;
public String xxx6;
public String xxx7;
public String xxx8;
public String xxx9;
public String xxx10;
}
And there was the following List.
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("eee");
Someone said. "I want you to loop through the list and set the fields in order from 1."
Developer A has implemented the following.
Xxx x = new Xxx();
int idx = 0;
x.xxx1 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx2 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx3 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx4 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx5 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx6 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx7 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx8 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx9 = (idx < list.size()) ? list.get(idx) : null;
idx++;
x.xxx10 = (idx < list.size()) ? list.get(idx) : null;
Developer B has implemented the following.
Xxx x = new Xxx();
for (int idx = 0; idx < list.size(); idx++) {
x.getClass().getField("xxx" + (idx + 1)).set(x, list.get(idx));
}
that's all.
Recommended Posts