[Code optimization] for-each replaces the common for loop or while LOOP, for-eachwhile

Source: Internet
Author: User

[Code optimization] for-each replaces the common for loop or while LOOP, for-eachwhile

The preferred method for traversing a set is for-each.

for(Element e :c){      doSomething(e);}

This is the practice after version 1.5; before Java, the Iterator was used.


To find out why it is better than a common for loop or whlie loop, take a look at the code.

Iterator <Element> I = c. iterator (); while (I. hasNext () {doSomething (I. next ();} Iterator <Element> i2 = c. iterator (); while (I. hasNext () {// bug should not call I doSomething (i2.next ());}

The above bug is because we often copy code with the same structure, but variables are used in the scope class and will not be found during compilation, and problems will occur during running.

This bug does not occur for the for loop. the variables of the first for loop cannot be used in the second for loop of the same method. Otherwise, the compilation fails,

This also shows that the for loop is better than the while loop.


For multi-loop iterations, even the old programmer is prone to a mistake.


Example: Card is used to represent the Card object, suit color, and rank points.

Collection <suit> suits = Arrays. asList (Suit. values); Collection <rank> ranks = Arrays. asList (Rank. values); List <card> cards = new ArrayList <Card> (); for (Iterator <Suit> I = suits. iterator (); I. hasNext () {for (Iterator <Rank> j = ranks. iterator (); j. hasNext () {cards. add (new Card (I. next, j. next); // note }}

Have you found the problem above ?? Many old programmers will suffer from such errors,
 cards.add(new Card(i.next,j.next));

Only j pieces of data can be obtained, instead of j pieces of data we want.


The correct method is as follows:

Collection <suit> suits = Arrays. asList (Suit. values); Collection <rank> ranks = Arrays. asList (Rank. values); List <card> cards = new ArrayList <Card> (); for (Iterator <Suit> I = suits. iterator (); I. hasNext () {Suit suit = I. next (); // with this added, the I loop should not be placed in j for (Iterator <Rank> j = ranks. iterator (); j. hasNext () {cards. add (new Card (suit, j. next); // note} then there is no need to worry about the double for-each. multiple iterations can be easily solved by using multiple for-each, the preceding bug <pre name = "code" class = "html"> for (Suit s: suits) {for (Rank r: ranks) {cards. add (s, r );}}

 

In this simple way, we can see that for-each is better than the common for loop and while loop.


Note:

However, for-each cannot be used in the following three cases:



In JAVA, what can be done by the while loop, and can be done by the for loop?

Not necessarily, this depends on the situation. The while loop is used only when you know the conditions, and the for loop requires you to know the number of cycles.
The use of the two is different, but the example on the second floor can also illustrate the problem, but it is not recommended to use this when writing code!
Remember that while is a "when" loop;

C language: in actual programming, the do-while loop can be replaced by the for loop.

Correct...

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.