繁体   English   中英

循环内循环 - 显示 ArrayList

[英]Loop inside loop - Display ArrayList

我正在尝试在 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>

你的问题是因为嵌套循环。 对于每个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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM