猿问

如何使用两个不同长度的数组打印表中的值

我想创建一个包含两个不同维度的不同数组的表,但执行后它没有给我正确的输出。


我有以下代码:


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 |

如何实现这一目标?


翻过高山走不出你
浏览 157回答 2
2回答

守候你守候我

我猜你会得到 Text1 | 文本 2 | 文本 3 | 对于每个标题。您不应该在每个循环中分配 j = 0。在两个 for 循环和内部 for 循环之外初始化一个 int index=0 应该是这样的。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++) {&nbsp; &nbsp; if(rowHeadingkeys!=null) {&nbsp; &nbsp; &nbsp; &nbsp; patientMonitoringTable.addCell(createCell(rowHeadingkeys[i],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PDFUtil.BOLD_FONT,1,Element.ALIGN_LEFT));&nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0;j<rowValuekeys.length/rowHeadingkeys.length;j++) {&nbsp; &nbsp; &nbsp; if(innerIndex < rowValuekeys.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; patientMonitoringTable.addCell(createCell(svo.getFields().get(rowValuekeys[innerIndex]).getValue(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PDFUtil.FONT,1,Element.ALIGN_LEFT));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;innerIndex++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

慕田峪9158850

如果您想要第 1 个项目的第 2 个列表中的 3 个项目:for(int i = 0; i < rowHeadingkeys.length;i++) {&nbsp; &nbsp; if(rowHeadingkeys!=null) {&nbsp; &nbsp; &nbsp; &nbsp; patientMonitoringTable.addCell(createCell(rowHeadingkeys[i],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PDFUtil.BOLD_FONT,1,Element.ALIGN_LEFT));&nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0; j < 3; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; patientMonitoringTable.addCell(createCell(svo.getFields().get(rowValuekeys[3 * i + j]).getValue(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PDFUtil.FONT,1,Element.ALIGN_LEFT));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答