Several ways of looping traversal

Source: Internet
Author: User

1. Basic

for (int i = 0; i<=50;i++) {

Todo

}

This loop is suitable for situations where you need to manipulate elements within a list based on an index.

2. Advanced

list<string> list = new arraylist<string> ();

for (String str:list) {

Todo

}

This loop is suitable for iterating through every element of the list, especially the list that iterates through an entity object.

3. High-order

list<string> list = new arraylist<string> ();

for (iterator<string> it = List.iterator (); It.hasnext ();) {

Strign str= It.next ();

if (It.hasnext ()) {

Todo

}else{

Todo

}

}

This loop is used when the list structure needs to be changed, the equivalent of the operation of the list, the only implementation of adding and removing nodes of the loop

Several ways of looping traversal

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.