Enhanced FOR Loop statement

Source: Internet
Author: User
Tags iterable

--Start

JDK 5.0 provides an enhanced for loop statement that makes it easy for us to iterate over algebraic groups and collections. In fact, if any class implements the Iterable interface, it can be traversed using an enhanced for loop statement, and here's a simple example.

Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;

public class Test {public

	static void Main (string[] args) throws Exception {
		Names Names = new Names ();
		Names.add ("Zhang San");
		Names.add ("Li Si");

		for (String name:names) {
			System.out.println (name);

}}} Class Names implements iterable<string> {
	private list<string> Names = new arraylist<string> (); c14/>public void Add (String name) {
		names.add (name);
	}

	Public iterator<string> iterator () {return
		names.iterator ();
	}
}

--- more See also:Java Fine Extract
-- statement: Reprint please indicate the source
--Last Updated on 2012-06-12
--written by Shangbo on 2012-06-12
--End

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.