算法问题(双重for循环)

时间:2021-08-09 20:29:00
求个高手给解决个关于for循环的问题
 问题大概是这样的:我从数据库取出来个List,然后我想把它循环显示到页面上去,而且要求每一行输出5个,多出5个的换行,如果最后剩下的少于5个,则全显示在一行中

我开始时这样写的,但是有溢出

<%
List list=(List)request.getSession().getAttribute("areaListForPageAll");

for(int i=0;i<list.size();i++){
%>
<TR>
<%
for(int j=0;j<2;j++){
i=i+j;
AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
%>
<TD>
<A class=pansy
href="#"><%=areaBaseInfo.getAreaName()%> </A>
</TD>
<% 
}
%>
</TR>
<%

}
%>

19 个解决方案

#1


错误时:

严重: Servlet.service() for servlet jsp threw exception
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.apache.jsp.jsp.dreamAfter.order_jsp._jspService(order_jsp.java:268)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:390)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:212)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)

#2


感觉上越界了

#3


i=i+j是什么意思

#4


java.lang.IndexOutOfBoundsException
索引越界,
你的i==list.size()-1
j==1的时候就越界了

#5


不要这样写。
你这个要实现的功能,最好的应该是使用取模的方式循环。
可以参考如下的伪代码:

for(int i=0;i<list.size();i++){
    if(i % 5 == 0){
        .... <br />
    }
}

#6


引用楼主 dailyblue 的回复:
HTML code<%
                                    List list=(List)request.getSession().getAttribute("areaListForPageAll");for(int i=0;i<list.size();i++){%><TR><%for(int j=0;j<2;j++){
                                            i=i+j;
                                            AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);%><TD><Aclass=pansyhref="#"><%=areaBaseInfo.getAreaName()%></A></TD><% 
                                            }%></TR><%            
                                    
                                    }%>


第一个循环的最大值为
 int size=list.size()%5==0?list.size():list.size()+1;

#7


里面的循环
for(int k=size;k<size+5;k++)

#8


。。。。

#9


引用 7 楼 qq271175778 的回复:
里面的循环
for(int k=size;k <size+5;k++)


能写个完整的么

#10


<%
List list=(List)request.getSession().getAttribute("areaListForPageAll");
%>
<TR>
<%
for(int i=0;i<list.size();i++){
AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
%>
<TD><A class=pansy href="#"><%=areaBaseInfo.getAreaName()%></A></TD>
<%if((i+1)%5==0){%></TR><TR>
<%}}%>
</TR>

							     

#11


public static void main(String[] args) {
for (int i = 0; i < 23; i++) {
if((i+1) % 5 == 0 ){
System.out.print(i + "  ");
System.out.println();
} else {
System.out.print(i + "  ");
}
}
}

控制台打印的效果:
0  1  2  3  4  
5  6  7  8  9  
10  11  12  13  14  
15  16  17  18  19  
20  21  22 


一行5個,最後一行不夠5就隻顯示剩下的
這樣解決你的問題了吧
你直接在頁面上套用就可以了

#12


List list=(List)request.getSession().getAttribute("areaListForPageAll");
                                    
                                    for(int i=0;i<list.size();i++){
                                %>
                                <TR>        
                                <%
                                       if((i+1)%5!=0){

                                            AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
                                %>
                                <TD>
                                    <A class=pansy
                                        href="#"><%=areaBaseInfo.getAreaName()%> </A>
                                </TD>
                                <% 
                                            }else {

                                %>
                                 </TR><TR>
                                <%AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
%>
 <TD>
                                    <A class=pansy
                                        href="#"><%=areaBaseInfo.getAreaName()%> </A>
                                </TD>
<%}%>
                                </TR>
                                <%            
                                    
                                    }
                                %>

#13


引用 12 楼 phh1989 的回复:
List list=(List)request.getSession().getAttribute("areaListForPageAll");
                                   
                                    for(int i=0;i <list.size();i++){
                                %>
                                <TR>       
                                <%
                                      if((i+1)%5!=0){

                                            AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
                                %>
                                <TD>
                                    <A class=pansy
                                        href="#"> <%=areaBaseInfo.getAreaName()%> </A>
                                </TD>
                                <%
                                            }else {

                                %>
                                </TR> <TR>
                                <%AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
%>
<TD>
                                    <A class=pansy
                                        href="#"> <%=areaBaseInfo.getAreaName()%> </A>
                                </TD>
<%}%>
                                </TR>
                                <%           
                                   
                                    }
                                %>


你的方法我放上去试了,不行的,显示出来的是每行只有一个
显示了15行

#14


11楼的可以

#15


11楼正解。

#16


以前写过一个,现在不在身边

#17


学习学习

#18


 <% 
   List list=(List)request.getSession().getAttribute("areaListForPageAll");
   for(int i=0;i<list.size();i++){
 %>
  <TR>        
     <TD>
         <A class=pansy href="#"><%=areaBaseInfo.getAreaName()%> </A>
     </TD>
<%  
   if(i%5==0){ 
%>
   </TR>
<%  
    }
}
%>
<%
  if(list.size()%5 != 0){
%>
     </TR>
  <%            
     }
   %>

这样就可以了

#19


 if(i%5==0) 改成if((i+1)%5==0)

#1


错误时:

严重: Servlet.service() for servlet jsp threw exception
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at org.apache.jsp.jsp.dreamAfter.order_jsp._jspService(order_jsp.java:268)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:390)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:212)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Unknown Source)

#2


感觉上越界了

#3


i=i+j是什么意思

#4


java.lang.IndexOutOfBoundsException
索引越界,
你的i==list.size()-1
j==1的时候就越界了

#5


不要这样写。
你这个要实现的功能,最好的应该是使用取模的方式循环。
可以参考如下的伪代码:

for(int i=0;i<list.size();i++){
    if(i % 5 == 0){
        .... <br />
    }
}

#6


引用楼主 dailyblue 的回复:
HTML code<%
                                    List list=(List)request.getSession().getAttribute("areaListForPageAll");for(int i=0;i<list.size();i++){%><TR><%for(int j=0;j<2;j++){
                                            i=i+j;
                                            AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);%><TD><Aclass=pansyhref="#"><%=areaBaseInfo.getAreaName()%></A></TD><% 
                                            }%></TR><%            
                                    
                                    }%>


第一个循环的最大值为
 int size=list.size()%5==0?list.size():list.size()+1;

#7


里面的循环
for(int k=size;k<size+5;k++)

#8


。。。。

#9


引用 7 楼 qq271175778 的回复:
里面的循环
for(int k=size;k <size+5;k++)


能写个完整的么

#10


<%
List list=(List)request.getSession().getAttribute("areaListForPageAll");
%>
<TR>
<%
for(int i=0;i<list.size();i++){
AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
%>
<TD><A class=pansy href="#"><%=areaBaseInfo.getAreaName()%></A></TD>
<%if((i+1)%5==0){%></TR><TR>
<%}}%>
</TR>

							     

#11


public static void main(String[] args) {
for (int i = 0; i < 23; i++) {
if((i+1) % 5 == 0 ){
System.out.print(i + "  ");
System.out.println();
} else {
System.out.print(i + "  ");
}
}
}

控制台打印的效果:
0  1  2  3  4  
5  6  7  8  9  
10  11  12  13  14  
15  16  17  18  19  
20  21  22 


一行5個,最後一行不夠5就隻顯示剩下的
這樣解決你的問題了吧
你直接在頁面上套用就可以了

#12


List list=(List)request.getSession().getAttribute("areaListForPageAll");
                                    
                                    for(int i=0;i<list.size();i++){
                                %>
                                <TR>        
                                <%
                                       if((i+1)%5!=0){

                                            AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
                                %>
                                <TD>
                                    <A class=pansy
                                        href="#"><%=areaBaseInfo.getAreaName()%> </A>
                                </TD>
                                <% 
                                            }else {

                                %>
                                 </TR><TR>
                                <%AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
%>
 <TD>
                                    <A class=pansy
                                        href="#"><%=areaBaseInfo.getAreaName()%> </A>
                                </TD>
<%}%>
                                </TR>
                                <%            
                                    
                                    }
                                %>

#13


引用 12 楼 phh1989 的回复:
List list=(List)request.getSession().getAttribute("areaListForPageAll");
                                   
                                    for(int i=0;i <list.size();i++){
                                %>
                                <TR>       
                                <%
                                      if((i+1)%5!=0){

                                            AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
                                %>
                                <TD>
                                    <A class=pansy
                                        href="#"> <%=areaBaseInfo.getAreaName()%> </A>
                                </TD>
                                <%
                                            }else {

                                %>
                                </TR> <TR>
                                <%AreaBaseInfo areaBaseInfo=(AreaBaseInfo)list.get(i);
%>
<TD>
                                    <A class=pansy
                                        href="#"> <%=areaBaseInfo.getAreaName()%> </A>
                                </TD>
<%}%>
                                </TR>
                                <%           
                                   
                                    }
                                %>


你的方法我放上去试了,不行的,显示出来的是每行只有一个
显示了15行

#14


11楼的可以

#15


11楼正解。

#16


以前写过一个,现在不在身边

#17


学习学习

#18


 <% 
   List list=(List)request.getSession().getAttribute("areaListForPageAll");
   for(int i=0;i<list.size();i++){
 %>
  <TR>        
     <TD>
         <A class=pansy href="#"><%=areaBaseInfo.getAreaName()%> </A>
     </TD>
<%  
   if(i%5==0){ 
%>
   </TR>
<%  
    }
}
%>
<%
  if(list.size()%5 != 0){
%>
     </TR>
  <%            
     }
   %>

这样就可以了

#19


 if(i%5==0) 改成if((i+1)%5==0)

#20