Loop inside loop - Display ArrayList

egx :

I'm trying to diaplay ArrayList in JSP. I have to ArrayLists I want to loop through and display the data in the same JSP table. 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:

<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

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

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

<% } %>

<%}}}%>
</tr>
</table>
Arvind Kumar Avinash :

Your problem is because of the nested loop. For each his, the complete inner for loop is executing which you do not want. You want that for each history element, the corresponding value from list1 is displayed.

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>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20897&siteId=1