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

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

Iceberg: The Future of Data Lake Tables

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

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?
