Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use for each in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to use for each in Java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In Java, traversing collections and arrays generally take the following three forms:

For (int I = 0; I < list.size (); iTunes +) {

System.out.print (list.get (I) + ",")

}

Iterator iter= list.iterator ()

While (iter.hasNext ()) {

System.out.print (iter.next () + ",")

}

For (Integer I: list) {

System.out.print (I + ",")

}

The first is ordinary for loop traversal, the second is traversal using iterators, and the third is commonly referred to as an enhanced for loop, or for each loop.

Analysis of the principle of for each implementation

We decompiled the following code:

For (String str:list) {

System.out.print (str+ ",")

}

The decompiled code looks like this:

String str

For (Iterator iterator = list.iterator (); iterator.hasNext (); System.out.print ((new StringBuilder (String.valueOf (str) .append (",") .toString ()) {

Str = (String) iterator.next ()

}

The decompiled code is a bit complex, but through decompilation, we can see that the underlying for each loop in Java is implemented through the iterator pattern.

Pit, be careful.

Since for each loops are implemented through iterators, they must have the characteristics of iterators. When using iterators to traverse elements in Java, it is important to note that when deleting the collection, ConcurrentModificationException may occur if you use it improperly, such as the following code:

For (String str:list) {

System.out.print (str+ ",")

List.remove (str)

}

A ConcurrentModificationException exception is thrown.

After Iterator is created, a single-linked index table pointing to the original object is created. When the number of the original object changes, the content of the index table does not change synchronously. When the index pointer moves back, no object to be iterated is found, so a ConcurrentModificationException exception is thrown.

Iterator does not allow iterated objects to be changed while working. However, you can use Iterator's own method remove () to delete objects, and the Iterator.remove () method will maintain index consistency while deleting the current iterative object.

Correctly delete the pose of the element while traversing:

Iterator Iter = list.iterator ()

While (Iter.hasNext ()) {

String str=Iter.next ()

System.out.print (str+ ",")

/ / notice that it is not list but the remove of Iter.

Iter.remove ()

}

This subtle problem may not occur in most cases due to the limitations of the conditions, and once the conditions are met, the problem will be exposed, which is why the code that runs well is suddenly reported wrong. So be sure to pay attention to this pit when using for each loops.

The above is all the contents of the article "how to use for each in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report