簡體   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