Preface writes
The for/in loop is the so-called enhanced for loop in JDK5.0, which traverses the array and the set, and uses it to make your code shorter and more refined. Here is a description of the following:
Traditional for loop and enhanced
Summarize:1, the disadvantage of For-each cycle: lost the index information.When traversing a collection or array, if you need to access the index of a collection or array, it is best to use the old-fashioned way to loop or traverse, rather than
1. An important theme of "JDK 5.0" is to simplify development by adding a few features, including:
• Static Import
• Automatic packaging/Unpacking
For-each Cycle
• Variable Parameters
• Enumerating
• generic type
• Meta data
static import Syntax is
For-each Cycle
The For-each loop is also called an enhanced for loop, or a foreach loop.
The For-each loop is a new feature of the JDK5.0 (other new features such as generics, automatic boxing, etc.).
The addition of the For-each loop simplifies
Enhanced for loop.
After checking on the Internet, the enhanced for loop is a new feature of java1.5. The so-called "enhanced for loop" mainly targets containers. When this feature is used, developers can hand over the logic of "traversing
first, the collectionThe method in the collection interface is a method that must be owned by all implementation classes in the collection.
ArrayList Implements List
List extends Collection
1. Basic UseOtherImport
Enhanced for Loop)The so-called "enhanced for loop" mainly targets containers. When this feature is used, developers canThe logic of traversing the container is handed over to the compiler for processing. For example, the following code:Void
JDK1.5 has been released for a long time and has been taken out because it adds a number of important features that help us simplify development, write code that is more concise and clear and secure, with the following features:? Static Import?
Iterator is an iterator interface (Interface) in Java that is used to provide standard Java iteratorsIterator supports generics because the type that the collection (Collection) class can mount is indeterminate, and the object type is removed from
The java1.5 version introduces an enhanced for loop, which is similar in principle to for loops.Syntax declaration: for (expression: conditional expression)Example: For(String str: set) Explanation: Set represents the set set, and STR represents
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.