Introduction to two for loops in Java

Source: Internet
Author: User

Copy codeThe Code is as follows: 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 closed query test class
* @ Author zhang
*
*/
Public class TestHouse {
Public static void main (String [] args ){
// Public member variables
SessionFactory sf = null;
Session session = null;
QueryProperty qp = new QueryProperty ();
// Encapsulate the queried data
Qp. setTitle ("% haofang % ");
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 statement
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 {
// Start Query
Sf = new Configuration (). configure (). buildSessionFactory ();
Session = sf. openSession ();
Query query = session. createQuery (sb. toString ());
Query. setProperties (qp );
List <House> list = query. list ();
// First use: loop
/* For (House house: list ){
System. out. println ("title:" + house. getTitle ());
System. out. println ("Area:" + house. getFloorage ());
System. out. println ("Price:" + house. getPrice ());
System. out. println ("zone:" + house. getStreet (). getDistrict (). getName ());
System. out. println ("the street is:" + house. getStreet (). getName ());
System. out. println ("----------------------------------");
}*/
// The second loop
For (int I = 0; I <list. size (); I ++ ){
System. out. println ("title:" + list. get (I). getTitle ());
System. out. println ("Area:" + list. get (I). getFloorage ());
System. out. println ("Price:" + list. get (I). getPrice ());
System. out. println ("zone:" + list. get (I). getStreet (). getDistrict (). getName ());
System. out. println ("the street is:" + list. get (I). getStreet (). getName ());
System. out. println ("----------------------------------");
}
} Catch (HibernateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Session. close ();
Sf. close ();
}
}
}

The first loop in the above example is not remembered, and the key character ":" is used. Generally, this loop is used to traverse a set (List <House>, Map) is very convenient.

The second type of loop is a normal loop. This is the most common for loop in Java.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.