教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 非常全面jsp页面的for循环示例程序

非常全面jsp页面的for循环示例程序

发布时间:2016-10-23   编辑:jiaochengji.com
教程集为您提供非常全面jsp页面的for循环示例程序等资源,欢迎您收藏本站,我们将为您提供最新的非常全面jsp页面的for循环示例程序资源
本文章整理了很多的jsp 页面的for循环示例程序,如果你对于java循环不怎么了解不防进入参考一下,非常全面的一篇文章

例子1

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6641')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6641>

<%
List<Object[]>  city=(List<Object[]>)request.getAttribute("list");
for(Object[] row:city){
%>
                     <tr>
                         <td width="404">
     <font color="#000000"><a href="#"><%=row[0] %></a> </font>
                         </td>
                         <td width="202">
     <font color="#000000"><%=row[1] %></font>
                         </td>
                         <td width="258">
    <font color="#000000"><%=row[2] %></font>
                         </td>
   
                     </tr>
                     <% }
%>

后台list 代码

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1455')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1455>

public List<Object[]> getAllInfos() {
 
  List<Object[]> list = new ArrayList<Object[]>();
        // 配置sql代码
String sql = "select  CityInfo.cityInfoContent,TypeInfo.typeInfoName, CityInfo.cityInfoDataTime  from TypeInfo,CityInfo where CityInfo.typeInfoID=TypeInfo.typeInfoID";
 
        Object[] obj = null;
 
        Result result = cityInfoDao.runSelect(sql, obj);
 
    if (result != null & result.getRowCount() > 0) {
 
      Map[] maps = result.getRows();
 
     for (Map row : maps) {
 
      String cityInfoContent = row.get("cityInfoContent").toString();
 
      String typeInfoName = row.get("typeInfoName").toString();
 
      String cityInfoDataTime = row.get("cityInfoDataTime").toString();
        
Object[] objects = { cityInfoContent, typeInfoName, cityInfoDataTime };
          
      list.add(objects);
       }
   }
        return list;
}

例子2

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4771')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4771>

package com.zxd.test;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.zxd.bean.House;
import com.zxd.util.QueryProperty;
/**
* HQL封闭查询的测试类
* @author zhang
*
*/
public class TestHouse {
public static void main(String[] args) {
//公共的成员变量
SessionFactory sf = null;
Session session = null;
QueryProperty qp = new QueryProperty();
//封装查询的数据
qp.setTitle("%好房%");
qp.setStreet_id("1002");
qp.setType_id("1004");
qp.setLow_price(20);
qp.setHigh_price(200);
qp.setSmall_floorage(50);
qp.setBig_floorage(180);
//HQL语句
StringBuffer sb = new StringBuffer();
sb.append("from House where ");
sb.append("(title like :title) ");
sb.append("and (type_id like :type_id) ");
sb.append("and (street_id like :street_id) ");
sb.append("and (price between :low_price and :high_price) ");
sb.append("and (floorage between :small_floorage and :big_floorage)");
try {
//开始执行查询
sf = new Configuration().configure().buildSessionFactory();
session = sf.openSession();
Query query = session.createQuery(sb.toString());
query.setProperties(qp);
List<House> list = query.list();
//第一种用:的循环
/*for(House house:list){
System.out.println("标题是:" house.getTitle());
System.out.println("面积是:" house.getFloorage());
System.out.println("价格是:" house.getPrice());
System.out.println("区是:" house.getStreet().getDistrict().getName());
System.out.println("街道是:" house.getStreet().getName());
System.out.println("----------------------------------");
}*/
//第二种循环
for(int i = 0;i<list.size();i ){
System.out.println("标题是:" list.get(i).getTitle());
System.out.println("面积是:" list.get(i).getFloorage());
System.out.println("价格是:" list.get(i).getPrice());
System.out.println("区是:" list.get(i).getStreet().getDistrict().getName());
System.out.println("街道是:" list.get(i).getStreet().getName());
System.out.println("----------------------------------");
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
session.close();
sf.close();
}
}
}

上面例子中的第一种循环是我没有记住的,用了关键字符“:”,一般这种循环是用来对一个集合的遍历上的(List<House>、Map)中的用的很方便


清单1:遍历数组的传统方式
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4337')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4337>

 /* 建立一个数组 */
 int[] integers = {1, 2, 3, 4};
 /* 开始遍历 */
 for (int j = 0; j < integers.length; j ) {
     int i = integers[j];
     System.out.println(i);
 }

而对于遍历Collection对象,这个循环则通常是采用这样的形式:
清单2:遍历Collection对象的传统方式
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7154')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7154>

/* 建立一个Collection */
 String[] strings = {"A", "B", "C", "D"};
 Collection stringList = java.util.Arrays.asList(strings);
 /* 开始遍历 */
 for (Iterator itr = stringList.iterator(); itr.hasNext();) {
     Object str = itr.next();
     System.out.println(str);
 }

而在Java语言的最新版本??J2SE 1.5中,引入了另一种形式的for循环。借助这种形式的for循环,现在可以用一种更简单地方式来进行遍历的工作。
 1、 第二种for循环
不严格的说,Java的第二种for循环基本是这样的格式:
 for (循环变量类型 循环变量名称 : 要被遍历的对象)  循环体
借助这种语法,遍历一个数组的操作就可以采取这样的写法:
清单3:遍历数组的简单方式
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7429')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7429>

 /* 建立一个数组 */
 int[] integers = {1, 2, 3, 4};

 /* 开始遍历 */
 for (int i : integers) {
     System.out.println(i); /* 依次输出“1”、“2”、“3”、“4” */
 }

这里所用的for循环,会在编译期间被看成是这样的形式:
清单4:遍历数组的简单方式的等价代码
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3519')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3519>

 /* 建立一个数组 */
 int[] integers = {1, 2, 3, 4};

 /* 开始遍历 */
 for (int 变量名甲 = 0; 变量名甲 < integers.length; 变量名甲 ) {
     System.out.println(integers[变量名甲]); /* 依次输出“1”、“2”、“3”、“4” */
 }

这里的“变量名甲”是一个由编译器自动生成的不会造成混乱的名字。
而遍历一个Collection的操作也就可以采用这样的写法:
清单5:遍历Collection的简单方式
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8019')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8019>

 /* 建立一个Collection */
 String[] strings = {"A", "B", "C", "D"};
 Collection list = java.util.Arrays.asList(strings);

 /* 开始遍历 */
 for (Object str : list) {
     System.out.println(str); /* 依次输出“A”、“B”、“C”、“D” */
 }

这里所用的for循环,则会在编译期间被看成是这样的形式:
清单6:遍历Collection的简单方式的等价代码
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3290')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3290>

 /* 建立一个Collection */
 String[] strings = {"A", "B", "C", "D"};
 Collection stringList = java.util.Arrays.asList(strings);

 /* 开始遍历 */
 for (Iterator 变量名乙 = list.iterator(); 变量名乙.hasNext();) {
     Object str = 变量名乙.next();
     System.out.println(str); /* 依次输出“A”、“B”、“C”、“D” */
 }

这里的“变量名乙”也是一个由编译器自动生成的不会造成混乱的名字。
因为在编译期间,J2SE 1.5的编译器会把这种形式的for循环,看成是对应的传统形式,所以不必担心出现性能方面的问题。
不用“foreach”和“in”的原因
Java采用“for”(而不是意义更明确的“foreach”)来引导这种一般被叫做“for-each循环”的循环,并使用“:”(而不是意义更明确的“in”)来分割循环变量名称和要被遍历的对象。这样作的主要原因,是为了避免因为引入新的关键字,造成兼容性方面的问题??在Java语言中,不允许把关键字当作变量名来使用,虽然使用“foreach”这名字的情况并不是非常多,但是“in”却是一个经常用来表示输入流的名字(例如java.lang.System类里,就有一个名字叫做“in”的static属性,表示“标准输入流”)。
的确可以通过巧妙的设计语法,让关键字只在特定的上下文中有特殊的含义,来允许它们也作为普通的标识符来使用。不过这种会使语法变复杂的策略,并没有得到广泛的采用。
 “for-each循环”的悠久历史
“for-each循环”并不是一个最近才出现的控制结构。在1979正式发布的Bourne shell(第一个成熟的UNIX命令解释器)里就已经包含了这种控制结构(循环用“for”和“in”来引导,循环体则用“do”和“done”来标识)。
2、防止在循环体里修改循环变量
在默认情况下,编译器是允许在第二种for循环的循环体里,对循环变量重新赋值的。不过,因为这种做法对循环体外面的情况丝毫没有影响,又容易造成理解代码时的困难,所以一般并不推荐使用。
Java提供了一种机制,可以在编译期间就把这样的操作封杀。具体的方法,是在循环变量类型前面加上一个“final”修饰符。这样一来,在循环体里对循环变量进行赋值,就会导致一个编译错误。借助这一机制,就可以有效的杜绝有意或无意的进行“在循环体里修改循环变量”的操作了。
清单7:禁止重新赋值
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9217')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9217>

 int[] integers = {1, 2, 3, 4};
 for (final int i : integers) {
     i = i / 2; /* 编译时出错 */
 }

注意,这只是禁止了对循环变量进行重新赋值。给循环变量的属性赋值,或者调用能让循环变量的内容变化的方法,是不被禁止的。
清单8:允许修改状态
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2091')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2091>

 Random[] randoms = new Random[]{new Random(1), new Random(2), new Random(3)};
 for (final Random r : randoms) {
     r.setSeed(4); /* 将所有Random对象设成使用相同的种子 */
     System.out.println(r.nextLong()); /* 种子相同,第一个结果也相同 */
 }

3. 类型相容问题
为了保证循环变量能在每次循环开始的时候,都被安全的赋值,J2SE 1.5对循环变量的类型有一定的限制。这些限制之下,循环变量的类型可以有这样一些选择:
循环变量的类型可以和要被遍历的对象中的元素的类型相同。例如,用int型的循环变量来遍历一个int[]型的数组,用Object型的循环变量来遍历一个Collection等。
清单9:使用和要被遍历的数组中的元素相同类型的循环变量
:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2176')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2176>

 int[] integers = {1, 2, 3, 4};
 for (int i : integers) {
     System.out.println(i); /* 依次输出“1”、“2”、“3”、“4” */
 }

清单10:使用和要被遍历的Collection中的元素相同类型的循环变量

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3415')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3415> Collection< String> strings = new ArrayList< String>();
 strings.add("A");
 strings.add("B");
 strings.add("C");
 strings.add("D");
 for (String str : integers) {
     System.out.println(str); /* 依次输出“A”、“B”、“C”、“D” */
 }

循环变量的类型可以是要被遍历的对象中的元素的上级类型。例如,用int型的循环变量来遍历一个byte[]型的数组,用Object型的循环变量来遍历一个Collection< String>(全部元素都是String的Collection)等。
清单11:使用要被遍历的对象中的元素的上级类型的循环变量

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9538')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9538> String[] strings = {"A", "B", "C", "D"};
 Collection< String> list = java.util.Arrays.asList(strings);
 for (Object str : list) {
     System.out.println(str);/* 依次输出“A”、“B”、“C”、“D” */
 }

循环变量的类型可以和要被遍历的对象中的元素的类型之间存在能自动转换的关系。J2SE 1.5中包含了“Autoboxing/Auto-Unboxing”的机制,允许编译器在必要的时候,自动在基本类型和它们的包裹类(Wrapper Classes)之间进行转换。因此,用Integer型的循环变量来遍历一个int[]型的数组,或者用byte型的循环变量来遍历一个Collection< Byte>,也是可行的。
清单12:使用能和要被遍历的对象中的元素的类型自动转换的类型的循环变量

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7285')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7285> int[] integers = {1, 2, 3, 4};
 for (Integer i : integers) {
     System.out.println(i); /* 依次输出“1”、“2”、“3”、“4” */
 }

注意,这里说的“元素的类型”,是由要被遍历的对象的决定的??如果它是一个Object[]型的数组,那么元素的类型就是Object,即使里面装的都是String对象也是如此。

您可能感兴趣的文章:
JSP常见问题
Python(for和while)循环嵌套及用法
jsp与php哪个快
ASP与JSP的比较(一)
Python for循环及用法详解
servlet与jsp基础教程(11)-JSP及语法概要
非常全面jsp页面的for循环示例程序
c#分支与循环结构的实例解析
win2k下asp php mysql jsp xsl xml的安装
JSP 怎么提高数据库访问效率

[关闭]
~ ~