public enum CostData {
IMPRESSIONS("Impressions", "Impressions"),
CLICKS("Clicks", "Clicks"),
COST("Expenditure", "Cost"),
CONVERSION("Conversion", "Conversions");
private String value1;
private String value2;
private CostData(String value1, String value2) {
this.value1 = value1;
this.value2 = value2;
}
public String getValue1() {
return value1;
}
public String getValue2() {
return value2;
}
}
我需要创建一个包含Impressions
, Clicks
, Expenditure
和Conversion
的数组,它们是每个枚举元素中的第一个元素,另一个数组在每个枚举元素中包含第二个集合。有什么建议吗?
如果我理解正确的话,你想做的是:
List<String> firstArray = new ArrayList<String>();
List<String> secondArray = new ArrayList<String>();
for(CostData c : CostData.values()) {
firstArray.add(c.getValue1());
secondArray.add(c.getValue2());
}
如果我做对了,你应该这样做。
int length = CostData.values().length;
String[] firstArray = new String[length];
String[] secondArray = new String[length];
int idx = 0;
for(CostData cd: CostData.values()){
firstArray[idx] = cd.getValue1();
secondArray[idx++] = cd.getValue2();
}