简体   繁体   English

循环内循环 - 显示 ArrayList

[英]Loop inside loop - Display ArrayList

I'm trying to diaplay ArrayList in JSP.我正在尝试在 JSP 中显示 ArrayList。 I have to ArrayLists I want to loop through and display the data in the same JSP table.我必须要遍历 ArrayLists 并将数据显示在同一个 JSP 表中。 This works fine, for the first Arralist (history), but when I get to the second it displays all the ArrayList in each row, instead of the current index from the loop:对于第一个 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>

Everything is fine until here.一切都很好,直到这里。 Instead of showing the current index from the loop, it displays all the ArrayList in each tr它不显示循环中的当前索引,而是显示每个 tr 中的所有 ArrayList

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

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

<% } %>

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

Your problem is because of the nested loop.你的问题是因为嵌套循环。 For each his , the complete inner for loop is executing which you do not want.对于每个his ,完整的内部for循环正在执行您不想要的。 You want that for each history element, the corresponding value from list1 is displayed.您希望为每个history元素显示list1的相应值。

Do it as follows:请按以下步骤操作:

<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