Home > Java > javaTutorial > Introduction to two for loops in Java

Introduction to two for loops in Java

高洛峰
Release: 2017-01-18 16:40:50
Original
1714 people have browsed it

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(); 
} 
} 
}
Copy after login

The first type of loop in the above example is something I didn't remember. It uses the key character ":". Generally, this type of loop is used to traverse a collection (List, Map) It's very convenient to use.

The second type of loop is an ordinary loop. This is the most common for loop in Java.

For more articles related to the introduction of two for loops in Java, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template