[Code optimization] For-each replaces the common for loop or while loop

Source: Internet
Author: User

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 = 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.


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

  1. Filter-If You Need To iterate the set and delete the selected elements, you must use the display iterator to call the Remove Method.
  2. Conversion -- this is not suitable if you need to traverse the list or array and modify or replace some elements in the list.
  3. Parallel conversion-control iterator or index variable that needs to be displayed to traverse multiple sets in parallel.

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.