Home > Java > javaTutorial > Compare the difference between foreach and for

Compare the difference between foreach and for

巴扎黑
Release: 2017-09-05 14:06:20
Original
2915 people have browsed it

The following editor will bring you a cliché about the difference between foreach (enhanced for loop) and for. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

First of all, let’s talk about foreach, which is also called enhanced for loop. Foreach is actually a special simplified version of for loop.

Let’s talk about the writing format of foreach:

for (element type element name: traverse array (set) (or iterable) ) {

Statement

}

Although foreach is a simplified version of the for loop, it does not mean that foreach is more useful than for. foreach is suitable for unknown loop times. Or it may be more efficient when calculating the number of loops is troublesome, but more complex loops still need to use for loops, which are more efficient.

Let’s look at the following example:


public static void main(String[] args) {
  List<String> arr = new ArrayList<String>();
  arr.add("你好");
  arr.add("我好");
  arr.add("大家好");
  
  //foreach循环
  for(String str : arr){       //这里的str就是为了获取每次循环的arr中的值
   System.out.println(str);       //就相当于 String str=arr[i]
  }
 }
Copy after login

But in comparison we use a for loop It’s a little troublesome to input


public static void main(String[] args) {
  List<String> arr = new ArrayList<String>();
  arr.add("你好");
  arr.add("我好");
  arr.add("大家好");
  
  //for循环
  for(int i=0;i<arr.size();i++){
   System.out.println(arr.get(i)); //要获取list中元素需要用get方法 
  }
 }
Copy after login

In addition to this ordinary collection, you can also use key-value pairs like map

For example:


public static void main(String[] args) {
  Map<String,String> mapstr = new HashMap<String,String>();
  mapstr.put("王", "男");
  mapstr.put("李", "男");
  mapstr.put("张", "女");
                      //entrySet方法是为了获取键值对的集合
  for(Map.Entry<String, String> s : mapstr.entrySet()){ //这里的Map.Entry<String, String>其实就是一个类型 用来表示键值对的类型
   System.out.println("key="+s.getKey());   //这里其实还是相当于 s=maostr.entrySet,只不过s存储的是键值对。
   System.out.println("value="+s.getValue());  //所以可以用get方法获取出来存储的键值对。
  }
 }
Copy after login

In addition, foreach does not support adding deletion operations in the loop, because when using the foreach loop, the array (collection) has been locked and cannot be modified, otherwise it will report java .util.ConcurrentModificationExceptionException

For example:


public static void main(String[] args) {
  List<String> arr = new ArrayList<String>();
  arr.add("你好");
  arr.add("我好");
  arr.add("大家好");
  
  //foreach循环
  for(String str : arr){     
   System.out.println(str);   
   arr.add("1");     //对arr进行添加 
  }
 }
Copy after login

We will talk about the principle of not being able to add and delete and how to add and delete in foreach in the next article.

So the summary is as follows:

foreach is suitable for just traversing a collection or array, while for is more efficient in more complex loops.

Foreach cannot modify the array or collection (add or delete operations). If you want to modify it, you must use a for loop.

So the for loop is more flexible in comparison.

The above is the detailed content of Compare the difference between foreach and for. For more information, please follow other related articles on 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