Reputation: 1
Hi I am trying to create a for loop in Java:
<%
for (int i = 0; i < webList.size(); i++) {
WebBean WebBean = (WebBean) webList.get(i);
System.out.println (i);
//for (int x = 0; x < webList.size(); x++) {
out.println( "<h2>SouthEast Teams</h2> " );
//}
%>
What I would like to do is split up the array at index 5 so that It creates two different list. I am currently using XML to create the list and it works okay except when I added in the heading for the second list "southeast teams" is appearing 8 times above the "northeast teams" heading and not after the team at index 5.
Below is my full JSP code:
<%
ArrayList webList = (ArrayList) request
.getAttribute(ConstantKeys.WEB_LIST);
%>
<h2 tabindex="0" id="contentBody">NorthEast Teams</h2>
<%
if (webList != null) {
%>
<table>
<%
for (int i = 0; i < webList.size(); i++) {
WebBean WebBean = (WebBean) webList.get(i);
System.out.println (i);
//for (int x = 0; x < webList.size(); x++) {
out.println( "<h2>SouthEast Teams</h2> " );
//}
%>
<tr>
<td class="col1">
<div class="buttonWrap"
title="<%=WebBean.getTeamName()%>" class="button"><%=WebBean.getTeamName()%></a></div>
</td>
<td tabindex="0"><%=WebBean.getLocation()%></td>
</tr>
<%
}
%>
</table>
<%
}
%>
Upvotes: 0
Views: 623
Reputation: 7070
Add A if statement that will check the how far you have iterated through the loop. (in this case you want to check 5)
edit: Add a variable that will be the "numberOfTeams" so you can change the value there and not in the if statement in case of future changes.
<%
for (int i = 0; i < webList.size(); i++) {
WebBean WebBean = (WebBean) webList.get(i);
if(i == 5){
System.out.println (i);
//for (int x = 0; x < webList.size(); x++) {
out.println( "<h2>SouthEast Teams</h2> " );
//}
}
%>
Upvotes: 1