In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the new features of Java1.5 foreach statement how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this new feature of Java1.5 foreach statement how to use the article will have a harvest, let's take a look.
I. brief introduction
Foreach statement is one of the new features in the Java1.5 version. The main function is to simplify the traversal function in the for loop, so it greatly facilitates the operation of programmers in traversing arrays and collections.
The specific format of foreach loop statement:
For (type variable name: collection) {statement block;} type, which represents the common type of each element in the collection.
The variable name, which represents each element in the collection.
Collection that represents the object or array to be traversed. II. Specific examples
Case one
Iterate through the array with the usual for loop statement:
Public class Demo03 {public static void main (String [] args) {/ / declare and initialize the array int [] nums= {1, 2, 3, 4, 5, 6, 7, 8, 9, System.out.println ("- for loop traversal -"), for (int I = 0; I < nums.length) ITunes +) {System.out.println ("th" + (item1) + ":" + nums [I]);}
Print the results:
-for loop traversal-
Number 1: 1
Number 2: 2
Number 3: 3
Number 4: 4
Number 5: 5
No. 6: 6
No. 7: 7
No. 8: 8
Number 9: 9
Iterate through the array with a foreach loop statement:
Public class Demo03 {public static void main (String [] args) {/ / declare and initialize the array int [] nums= {1 Count is: "+ item);}
Print the results:
-foreach loop traversal-
Count is:1
Count is:2
Count is:3
Count is:4
Count is:5
Count is:6
Count is:7
Count is:8
Count is:9
From the above comparison, you can see that foreach does not need to get the length of the array to traverse, nor does it have to access the elements of the array based on the index. Compared with for loop statements, foreach loop statements do not need loop conditions, nor do they need loop iteration statements, which have been handed over to the system to complete.
So can the foreach statement replace the for loop statement? The answer is not so. At present, foreach statements can not completely replace for loop statements, but any foreach statement can be rewritten into for loop statements.
This is the end of the article on "how to use foreach statements, a new feature of Java1.5". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use foreach sentences, the new features of Java1.5". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.