Home > Java > javaTutorial > body text

In-depth understanding of for and foreach loops in java

高洛峰
Release: 2017-01-21 15:47:03
Original
1846 people have browsed it

•The variable in the loop condition in the for loop only evaluates once! See the last picture for details

•The foreach statement is new in java5. When traversing arrays and collections, foreach has good performance.

•foreach is a simplification of the for statement, but foreach cannot replace the for loop. It can be said that any foreach can be rewritten as a for loop, but the reverse does not work.

•foreach is not a keyword in java. The loop object of foreach is generally a collection, List, ArrayList, LinkedList, Vector, array, etc.

•Foreach format:

for (element type T, name of each loop element O: loop object){

      //Operation on O

  }

1. Common usage methods.

1. foreach traverses the array.

/**
 * 描述:
 * Created by ascend on 2016/7/8.
 */
public class Client {
  public static void main(String[] args) {
    String[] names = {"beibei", "jingjing"};
    for (String name : names) {
      System.out.println(name);
    }
  }
}
Copy after login

2.foreach traverses List.

/**
 * 描述:
 * Created by ascend on 2016/7/8.
 */
public class Client {
 
  public static void main(String[] args) {
    List<String> list = new ArrayList();
    list.add("a");
    list.add("b");
    list.add("c");
    for(String str : list){
      System.out.println(str);
    }
  }
}
Copy after login

2. Limitations.

Although foreach can traverse arrays or collections, it can only be used to traverse and cannot modify the array or collection during the traversal process, while the for loop can modify the source array or collection during the traversal process. .

1. Array

/**
 * 描述:
 * Created by ascend on 2016/7/8.
 */
public class Client {
 
  public static void main(String[] args) {
    String[] names = {"beibei", "jingjing"};
    for (String name : names) {
      name = "huanhuan";
    }
    //foreach
    System.out.println("foreach:"+Arrays.toString(names));
    //for
    for (int i = 0; i < names.length; i++) {
      names[i] = "huanhuan";
    }
    System.out.println("for:"+Arrays.toString(names));
  }
}
Copy after login

Output:

foreach:[beibei, jingjing]

for:[huanhuan, huanhuan]

2. Collection

/**
 * 描述:
 * Created by ascend on 2016/7/8.
 */
public class Client {
 
  public static void main(String[] args) {
    List<String> names = new ArrayList<String>();
    names.add("beibei");
    names.add("jingjing");
    //foreach
    for(String name:names){
      name = "huanhuan";
    }
    System.out.println(Arrays.toString(names.toArray()));
    //for
    for (int i = 0; i < names.size(); i++) {
      names.set(i,"huanhuan");
    }
    System.out.println(Arrays.toString(names.toArray()));
  }
}
Copy after login

Output:

[beibei, jingjing]

[huanhuan, huanhuan]

A place to pay special attention to! !

In-depth understanding of for and foreach loops in java

The above article has an in-depth understanding of the for and foreach loops in Java. This is all the content shared by the editor. I hope it can give you a reference, and I hope you will support PHP more. Chinese website.

For more in-depth understanding of for and foreach loops in Java, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!