【问题标题】:jsp 2D array in table表格中的jsp二维数组
【发布时间】:2022-07-08 00:13:13
【问题描述】:

我在 java 中有一个二维数组,试图在 jsp 中以表格形式打印它。 下面的代码正在运行 -

                                <table>
                                    <c:forEach items="${combinations}" var="face">
                                        <tr>
                                            <td>${face[0]}</td>
                                            <td>${face[1]}</td>
                                            <td>${face[2]}</td>
                                        </tr>
                                    </c:forEach>
                                </table>

但我需要根据数组的大小使其动态化,而不是硬编码索引。 请帮忙。

【问题讨论】:

    标签: java jsp jstl


    【解决方案1】:

    你可以像在普通 java 中一样使用 for lops,如下所示:

    <table>
    <%
    int[][] arr = { {0,1,2}, {3,4,5}, {6,7,8}};
    for (int rowIndex = 0; rowIndex < arr.length; rowIndex++) {
        %><tr><%
        for (int numIndex = 0; numIndex < arr[rowIndex].length; numIndex++) {
            %><td><%= arr[rowIndex][numIndex] %></td><% 
        }
        %></tr><%
    }
    %>
    </table>
    

    您只需确保代码的每个元素都在 &lt;% ... %&gt; 语句中。

    此代码的输出如下所示:

    【讨论】:

      猜你喜欢
      • 2015-10-25
      • 1970-01-01
      • 2019-01-14
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      相关资源
      最近更新 更多