In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "ArrayList trap case analysis in Java". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "ArrayList trap case analysis in Java" can help you solve the problem.
The problem analysis is full of doubts.
When Xiaofeng heard this interview question, she thought to herself what kind of water interviewer this is, how to ask such a simple question, and think of a for loop plus equal judgment and then delete it. But on second thought, no, there must be a trap, otherwise you wouldn't have asked such a seemingly simple question. Xiao Feng suddenly remembered that she had encountered this problem when she wrote the code before. She also deleted the specified element in ArrayList, but she also threw an exception when she looped the remove element directly. The interviewer's trap is probably here. Xiaofeng secretly rejoiced and found the trap laid by the interviewer. Xiaofeng recalled the test that day, and the code was desensitized. At the beginning, I wanted to delete the specified element in ArrayList. Xiao Feng wrote down the following code three times and five times, and wrote down the following code. I confidently clicked the button of the Run code. The result was embarrassed and an exception was thrown.
Public class TestListMain {public static void main (String [] args) {List result = new ArrayList (); result.add ("a"); result.add ("b"); result.add ("c"); result.add ("d"); for (String s: result) {if ("b" .equals (s)) {result.remove ("b") }
A big red exception will come out soon, OMG, how can this be? there is nothing wrong with the code. Let's see what exception is thrown and where the exception is thrown. You can see that a ConcurrentModificationException exception was thrown, and it was thrown in a detection method in the Itr class. What's going on? We don't have this Itr code in our original code, which is really puzzling.
restore justice
Since the source code analysis can not come out, let's take a look at the source code compiled in the class file is what it is, after all, the class file is the real implementation of JVM code, do not see do not know, a look startled, JDK is originally played in this way. I see, the for-each statements in our original code are actually executed by iterators instead of being compiled.
Public class TestListMain {public TestListMain () {} public static void main (String [] args) {List result = new ArrayList (); result.add ("a"); result.add ("b"); result.add ("c"); result.add ("d"); / / create iterator Iterator var2 = result.iterator () While (var2.hasNext ()) {String s = (String) var2.next (); if ("b" .equals (s)) {result.remove ("b");}
Through the internal class iterator Itr created by ArrayList, the for-each loop is transformed into an iterator plus a while loop, and the original for-each loop is sold with a sheep's head.
Public Iterator iterator () {return new Itr ();}
Itr, an inner class iterator, determines whether the iterator has content by judging hasNext (), while the next () method gets the content in the iterator.
Private class Itr implements Iterator {int cursor; / / index of next element to return int lastRet =-1; / / index of last element returned;-1 if no such int expectedModCount = modCount; Itr () {} public boolean hasNext () {return cursor! = size;} @ SuppressWarnings ("unchecked") public E next () {checkForComodification () Int I = cursor; if (I > = size) throw new NoSuchElementException (); Object [] elementData = ArrayList.this.elementData; if (I > = elementData.length) throw new ConcurrentModificationException (); cursor = I + 1; return (E) elementData [lastRet = I];}.}
The general process is as follows:
What really throws an exception is this detection method, which throws an exception directly when modCount is not equal to expectedModCount. So we need to see what modCount and expectedModCount are respectively. Here modCount represents the number of changes to ArrayList, and expectedModCount represents the number of changes to the iterator. When creating the Itr iterator, modCount is assigned to expectedModCount, so in this example, both modCount and expectedModCount are 4 (four String elements have been added). But after getting the b element, ArrayList does the remove operation, so the modCount adds up to 5. Therefore, there is an inconsistency during the inspection, which eventually leads to an exception. At this point, we have found the reason for throwing the exception, using an iterator for the loop, but the operation element is an ArrayList operation, so the iterator finds that the element has been modified during the loop and throws an exception.
Let's think about it again, why do we have this test? What exactly does this anomaly do? Let's start with how ConcurrentModificationException's comments describe it. The simple understanding is that one thread is not allowed to modify the collection, and the other thread iterates on the basis of the collection. Once this condition is detected, an exception is thrown through the fast-fail mechanism to prevent subsequent unknowns.
/ * For example, it is not generally permissible for one thread to modify a Collection * while another thread is iterating over it. In general, the results of the * iteration are undefined under these circumstances. Some Iterator * implementations (including those of all the general purpose collection implementations * provided by the JRE) may choose to throw this exception if this behavior is * detected. Iterators that do this are known as fail-fast iterators, * as they fail quickly and cleanly, rather that risking arbitrary, * non-deterministic behavior at an undetermined time in the future. * / public class ConcurrentModificationException extends RuntimeException {...} how to delete correctly
Since the reason for throwing an exception is the recycling of iterators, deleting the use of ArrayList causes the detection to fail. Then we recycle the iterator and delete it with the iterator, so that we can ensure consistency.
Public class TestListMain {public static void main (String [] args) {List result = new ArrayList (); result.add ("a"); result.add ("b"); result.add ("c"); result.add ("d"); Iterator iterator = list.iterator () While (iterator. HasNext ()) {String str = iterator.next (); if ("b" .equals (str)) {iterator.remove () } this is the end of the introduction to "ArrayList Trap instance Analysis in Java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.