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 analyze the Itr class in Iterator

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail how to analyze the Itr class in Iterator. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

All right, let's get down to business and see why he's wrong and what's wrong with him.

Look at the code first:

one

What's wrong?

Obviously, he reported the error at line 36, that is, after traversing the data with a value of 3. Let's move forward, why did we report an error after traversing the second element, because after traversing the data with a value of 3, he added a data with a value of 12 to the list.

Let's try to get rid of the if judgment in the traversal, and the answer is yes. Then we found the reason, that is, an element was added during the traversal, which led to his error.

two

What's wrong?

Let's take a look at the source code in ArrayList and what he does in the add method, which causes him to report an error when traversing.

Figure 1:

Figure 2:

Figure 3:

Figure 4:

The above four figures are all hierarchical calls, that is, when executing the OK button, first determine the size of the list array. If the ensureCapacityInternal method is an empty array, take the constant DEFAULT_CAPACITY in ArrayList as the container size, then increase the number of modifications modCount, and finally compare the minimum capacity and the length of the array to consider the expansion. Judging from the description just now, he is only increasing the number of modifications modCount and considering whether or not to expand the capacity.

So let's take a look at what modCount is and where it is used, let's go back to this topic.

In line 33, an iterator is defined on the list array. Let's follow the source code and take a look.

That is, he created an Itr class that includes three variables of cursor,lastRet,expectedModCount and three methods of hasNext,next,remove.

So let's popularize the meaning of these three variables. Cursor represents the subscript of the next access element, lastRet represents the subscript of the last access element, and expectedModCount indicates the expected number of changes.

Both the next method and the remove method call the checkForComodification method at the beginning, and this method determines whether the number of modCount and expectedModCount is the same. If it is different, an exception is thrown. Obviously, the error message when we start running the code coincides with the exception. In fact, we know that the exception information he threw there caused the program to report an error, that is, modCount and expectedModCount are different.

three

Breakpoint debugging

Let's sort out the whole process from scratch and debug the whole code at a breakpoint.

First of all, he constructs an array and adds 1d3 data to it. He has seen what is in the add method above, and there is an increase in the value of modCount in the add method, so here the modCount is already 3. Then we define an iterator, and we just know that we have actually created a new Itr class, so let's see what the value of iterator is at the end of line 33.

The next subscript cursor to be accessed is 0, the previous subscript lastRet is-1, and the expected number of modifications expectedModCount is 3, which is equal to the size of his array size, that is, 3.

Then enter line 35 to start the iterative array, execute the hasNext method, cursor is 0, not equal to size, is true, continue to execute the next method, first determine whether modCount (that is, 3) is equal to expectedModCount (that is, 3), the answer is equal, and then output the value 1.

Then go to line 35 to start the iterative array, execute the hasNext method, cursor is 1, not equal to size, true, continue to execute the next method, first determine whether modCount (that is, 3) is equal to expectedModCount (that is, 3), the answer is equal, and then output the number 3. Note that there is an if judgment here. There is an add method, modCount is to add 1, and now modCount has become 4.

Then go to line 35 to start the iterative array, execute the hasNext method, cursor is 2, not equal to size, is true, continue to execute the next method, first determine whether modCount (that is, 4) is equal to expectedModCount (that is, 3), the answer is not equal, throw an exception.

The whole process is over.

ArrayList cannot be modified during traversal, including additions and deletions. That is, thread is not safe. If another thread modifies the lsit during traversal, it will throw an exception. This is the fast-fail (Quick failure Strategy). The embodiment of this strategy in the source code is that when the next method is used, the checkForComodification method is called. By comparing whether modCount and expectedModCount are equal, to determine whether there is a modification operation during traversal, so as to determine whether an exception should be thrown. Then when you need to ensure that the array is not modified when traversing, you can give priority to using iterator to traverse.

On how to carry out the analysis of the Itr class in Iterator is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report