【问题标题】:Loop inside loop - Display ArrayList循环内循环 - 显示 ArrayList
【发布时间】:2020-06-15 18:45:37
【问题描述】:

我正在尝试在 JSP 中显示 ArrayList。我必须要遍历 ArrayLists 并在同一个 JSP 表中显示数据。对于第一个 Arralist(历史),这很好用,但是当我到达第二个时,它会显示每行中的所有 ArrayList,而不是循环中的当前索引:

<table id="test" class="table text">
<tr>
    <th>Result</th>
    <th>Deposit</th>
    <th>Bet</th>
    <th>Return +/-</th>
    <th>Current balance</th>
    <th>Date</th>
</tr>
<%


for (history his : history) {

%>
<tr class="listing">


            <td><%=his.getRes()%></td>
            <td><%=resultTwoDecimalsDeposit%></td>
            <td><%=his.getOdds()%></td>
            <td><%=his.getDate() %> </td>
            <td style="color:#00cc00;"> + <%=resultTwoDecimalsRet%> </td>

到这里为止一切都很好。它不显示循环中的当前索引,而是显示每个 tr 中的所有 ArrayList

<%  for (int i = 0; i < list1.size(); i++) {    
%>

<td><%=list1.get(i) %></td>

<% } %>

<%}}}%>
</tr>
</table>

【问题讨论】:

  • 您是否在for 之前添加了&lt;tr&gt;?请edit您的问题并包括一个真实的minimal reproducible example,而不仅仅是代码部分。
  • 我没有放任何其他 然后在示例中显示 -> 我总共有 2 个 。
  • 如果你只有一个&lt;tr&gt;和一个&lt;/tr&gt;,那么这就是一行输出。

标签: java loops jsp for-loop arraylist


【解决方案1】:

您的问题是由于嵌套循环。对于每个his,您不希望执行完整的内部for 循环。您希望为每个 history 元素显示来自 list1 的相应值。

按如下方式进行:

<table id="test" class="table text">
    <tr>
        <th>Result</th>
        <th>Deposit</th>
        <th>Bet</th>
        <th>Return +/-</th>
        <th>Current balance</th>
        <th>Date</th>
    </tr>
    <%for (int i=0; i<history.size() && i<list1.size(); i++) {%>
        <tr class="listing">
            <td><%=history.get(i).getRes()%></td>
            <td><%=resultTwoDecimalsDeposit%></td>
            <td><%=history.get(i).getOdds()%></td>
            <td><%=history.get(i).getDate() %> </td>
            <td style="color:#00cc00;"> + <%=resultTwoDecimalsRet%> </td>           
            <td><%=list1.get(i) %></td>
        </tr>
    <%}%>           
</table>

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签