Java 跳过for循环中的元素并重新分配它
我正在JasperSoft中填充参数。在我的报告中,我有以下参数: 参数_1、参数_2、参数_3Java 跳过for循环中的元素并重新分配它,java,loops,for-loop,foreach,Java,Loops,For Loop,Foreach,我正在JasperSoft中填充参数。在我的报告中,我有以下参数: 参数_1、参数_2、参数_3 int a; for (a = 0; a < headers.length; a++) { parameters.put("Parameter_" + a, headers[a]); } 我从第二个代码位得到了什么 Parameter_1 = name Parameter_2= Parameter_GroupBy= date Parameter_3= stre
int a;
for (a = 0; a < headers.length; a++) {
parameters.put("Parameter_" + a, headers[a]);
}
我从第二个代码位得到了什么
Parameter_1 = name
Parameter_2=
Parameter_GroupBy= date
Parameter_3= street
我想按日期分组(参数_2),所以我想
Parameter_1 = name
Parameter_2= street
Parameter_GroupBy= date
Parameter_3=
如何做到这一点?使用JDK 1.6和Windows。在我看来,您只需要为“下一个要放置的参数”保留一个单独的索引:
int参数索引=1;
//注意:声明迭代变量更为惯用
//圈内
对于(int-headerIndex=0;headerIndex
Parameter_1 = name
Parameter_2=
Parameter_GroupBy= date
Parameter_3= street
Parameter_1 = name
Parameter_2= street
Parameter_GroupBy= date
Parameter_3=
int parameterIndex = 1;
// Note: more idiomatic to declare the iteration variable
// inside the loop
for (int headerIndex = 0; headerIndex < headers.length; headerIndex++) {
String header = headers[headerIndex];
if (headerIndex == groupBy) {
parameters.put("Parameter_GroupBy", header);
} else {
parameters.put("Parameter_" + parameterIndex, header);
parameterIndex++;
}
}