我想用两个具有不同维数的不同数组创建一个表,但是执行后不能给我正确的输出。
我有以下代码:
String rowHeadingkeys[] = new String[] {"heading1","heading2","heading3","heading4","heading5","heading6","heading7","heading8"};
String rowValuekeys[] = new String[] {"Text1","Text2","Text3","Text4","Text5",
"Text6","Text7","Text8","Text9","Text10",
"Text11","Text12","Text13","Text14","Text15",
"Text16","Text17","Text18","Text19","Text20",
"Text21","Text22","Text23","Text24"};
for(int i = 0;i<rowHeadingkeys.length;i++) {
if(rowHeadingkeys!=null) {
patientMonitoringTable.addCell(createCell(rowHeadingkeys[i],
PDFUtil.BOLD_FONT,1,Element.ALIGN_LEFT));
for(int j = 0;j<rowValuekeys.length/rowHeadingkeys.length;j++) {
patientMonitoringTable.addCell(createCell(svo.getFields().get(rowValuekeys[j]).getValue(),
PDFUtil.FONT,1,Element.ALIGN_LEFT));
}
}
}
我想使其如下所示:
| heading1 | Text1 | Text2 | Text3 |
| heading2 | Text4 | Text5 | Text6 |
| heading3 | Text7 | Text8 | Text9 |
| heading4 | Text10 | Text12 | Text13 |
| heading5 | Text14 | Text15 | Text16 |
| heading6 | Text17 | Text18 | Text19 |
| heading7 | Text20 | Text21 | Text22 |
| heading8 | Text23 | Text24 | Text24 |
如何实现?
答案 0 :(得分:0)
我猜您正在获取Text1 |文字2 |文字3 |每个标题。
您不应该在每个循环中分配j = 0。在两个for循环之外初始化一个int index = 0,内部for循环应如下所示。
for(int j = index; j<index + (rowValuekeys.length/rowHeadingkeys.length);j++){
}
index+=rowValuekeys.length/rowHeadingkeys.length;
编辑:
更好的解决方案是:
int innerIndex = 0;
for(int i = 0;i<rowHeadingkeys.length;i++) {
if(rowHeadingkeys!=null) {
patientMonitoringTable.addCell(createCell(rowHeadingkeys[i],
PDFUtil.BOLD_FONT,1,Element.ALIGN_LEFT));
for(int j = 0;j<rowValuekeys.length/rowHeadingkeys.length;j++) {
if(innerIndex < rowValuekeys.length)
patientMonitoringTable.addCell(createCell(svo.getFields().get(rowValuekeys[innerIndex]).getValue(),
PDFUtil.FONT,1,Element.ALIGN_LEFT));
innerIndex++;
}
}
}
答案 1 :(得分:0)
如果要从第二个列表中为第一项的每个项选择3个项:
for(int i = 0; i < rowHeadingkeys.length;i++) {
if(rowHeadingkeys!=null) {
patientMonitoringTable.addCell(createCell(rowHeadingkeys[i],
PDFUtil.BOLD_FONT,1,Element.ALIGN_LEFT));
for(int j = 0; j < 3; j++) {
patientMonitoringTable.addCell(createCell(svo.getFields().get(rowValuekeys[3 * i + j]).getValue(),
PDFUtil.FONT,1,Element.ALIGN_LEFT));
}
}
}