Enhanced for loop foreach in Java

Source: Internet
Author: User

foreach is a syntactic sugar in Java, and almost every language has some of these syntactic sugars to make it easier for programmers to develop and process these grammars in specific bytecode or specific ways during compilation. Can improve performance and reduce the chance of code errors. Also in Java are generics, auto-unpacking, auto-boxing, inner classes, enumerations, and so on.

foreach is the syntax used to traverse an array or collection. The specific syntax is as follows:

?
1 2 3 for(元素类型 ele : 数组名/Iterable 实例){ }

Here we use foreach to iterate over the array and a collection:

?
1 2 3 4 5 6 7 8 9 one int [] array = {1,2,3};for(int i : array){  System.out.println(i);}     List list = new ArrayList();list.add(1);list.add(2);list.add(3);for(Object obj : list){    System.out.println(obj);}

Then we can view the contents of the class file through the anti-compilation tool:

?
123456789Ten One A - - the - - - + - + A int array[] = {1,2,3};   int [] array$ = array;   for(int len$ = array$.length, i$ = 0; i$<len$; ++i$ )   {       int i = array$[i$];       {           System.out.println(i);       }   }           List list = new ArrayList();   list.add(1);   list.add(2);   list.add(3);   for(java.util.Iterator i$ = list.iterator(); i$.hasNext();)   {       String s = (String) i$.next();       {           System.out.println(s);       }   }

It is clear that:

1. For arrays, the Foreach loop actually uses the normal for loop

2. For collections, the Foreach loop is actually an iterative iteration of the iterator iterator

Note: If we want to iterate, delete the elements in the collection as follows:

?
1 2 3 4 5 6 7 List list = new ArrayList();list.add(1);<br>    list.add(2);   list.add(3);for(Object obj : list){  System.out.println(obj);  list.remove(obj);  //一边迭代一边删除}

This will report the following error: This is a concurrent modification exception errors

Cause: When the iterator is running, a thread B is created separately in the current thread A. A is responsible for continuing the iteration, and the B thread is responsible for deletion. Each time the B thread checks to see if the elements in the a thread are the same, and if not, the error

Because the method removed above is using the Remove () method in the collection of Collection (the parent class of ArrayList). The method can only remove elements from the collection, and it cannot remove the elements from the iterator.

WORKAROUND: Use the Remove () method in the iterator iterator

?
1 2 3 4 5 6 Iterator it = list.iterator();while(it.hasNext()){  Object obj = it.next();  System.out.println(obj);  if(obj.equals(1)){    it.remove();//这里是用 迭代器的 remove() 方法<br><br>       //list.remove(obj); <br>       //如果你用 集合 方法,那么还是会报错<br>    }    <br>  }

  

Enhanced for loop foreach in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.