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

In-depth understanding of for and foreach loops in java

Jan 21, 2017 pm 03:47 PM

•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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20: Key Performance Boosts and New Features

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed Mar 07, 2025 pm 05:52 PM

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

How can I implement functional programming techniques in Java? How can I implement functional programming techniques in Java? Mar 11, 2025 pm 05:51 PM

How can I implement functional programming techniques in Java?

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

See all articles