Java 循环内部循环-显示阵列列表
我正在尝试在JSP中显示ArrayList。我必须排列我想要循环的列表,并在同一个JSP表中显示数据。对于第一个Arralist(history),这很好,但是当我到达第二个Arralist时,它会显示每行中的所有ArrayList,而不是循环中的当前索引:Java 循环内部循环-显示阵列列表,java,loops,jsp,for-loop,arraylist,Java,Loops,Jsp,For Loop,Arraylist,我正在尝试在JSP中显示ArrayList。我必须排列我想要循环的列表,并在同一个JSP表中显示数据。对于第一个Arralist(history),这很好,但是当我到达第二个Arralist时,它会显示每行中的所有ArrayList,而不是循环中的当前索引: <table id="test" class="table text"> <tr> <th>Result</th> <th>Deposit</th>
<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
内部循环,这是您不想要的。您希望为每个历史
元素显示列表1
中的相应值
按如下方式操作:
<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>
结果
押金
打赌
返回+/-
电流平衡
日期
您是否在
的之前添加了
?请回答您的问题,并包括一个真实的部分,而不仅仅是代码部分。我没有在示例中列出任何其他部分->我总共有2个。如果您只有一个
和一个
,则这是一行输出。