Home >Java >javaTutorial >In-depth understanding of for and foreach loops in java

In-depth understanding of for and foreach loops in java

高洛峰
高洛峰Original
2017-01-21 15:47:031859browse

•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);
    }
  }
}

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);
    }
  }
}

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));
  }
}

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()));
  }
}

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!

Statement:
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