when modCount is initialized in java.util.ArrayList?
I want to know when the field modCount of java.util.ArrayList is initialized.From the source code of java.util.ArrayList,we know the field modCount is inherited from
java.util.AbstractList. And in the private inner class of java.util.ArrayList named Itr,its field expectedModCount is assigned from modCout,as shown below
In a demo,I debugged and found that itr.expectedModCount has been initialized.Because the value of expectedModCount is from modCount.So I looked up the source code to find when modCount is initialized,but failed.
java iterator
add a comment |
I want to know when the field modCount of java.util.ArrayList is initialized.From the source code of java.util.ArrayList,we know the field modCount is inherited from
java.util.AbstractList. And in the private inner class of java.util.ArrayList named Itr,its field expectedModCount is assigned from modCout,as shown below
In a demo,I debugged and found that itr.expectedModCount has been initialized.Because the value of expectedModCount is from modCount.So I looked up the source code to find when modCount is initialized,but failed.
java iterator
add a comment |
I want to know when the field modCount of java.util.ArrayList is initialized.From the source code of java.util.ArrayList,we know the field modCount is inherited from
java.util.AbstractList. And in the private inner class of java.util.ArrayList named Itr,its field expectedModCount is assigned from modCout,as shown below
In a demo,I debugged and found that itr.expectedModCount has been initialized.Because the value of expectedModCount is from modCount.So I looked up the source code to find when modCount is initialized,but failed.
java iterator
I want to know when the field modCount of java.util.ArrayList is initialized.From the source code of java.util.ArrayList,we know the field modCount is inherited from
java.util.AbstractList. And in the private inner class of java.util.ArrayList named Itr,its field expectedModCount is assigned from modCout,as shown below
In a demo,I debugged and found that itr.expectedModCount has been initialized.Because the value of expectedModCount is from modCount.So I looked up the source code to find when modCount is initialized,but failed.
java iterator
java iterator
asked Nov 14 '18 at 13:20
Zhi ZhengZhi Zheng
414
414
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It is initialized to 0 (note that it's an instance variable of the AbstractList
super-class):
protected transient int modCount = 0;
and incremented in several places in which the List
is structurally modified (i.e. elements are added or removed).
For example:
public E remove(int index)
rangeCheck(index);
modCount++;
....
Note that the Itr
class is an inner class of ArrayList
, and therefore it has access to the members of the enclosing ArrayList
instance (which includes the modCount
instance variable of the AbstractList
super-class).
Thanks for your help.But I still have a doubt when I debugged,I found taht the expectedModCount instance varaible of the Itr class did not equal to the value of modCount instance variable of the ArrayList.Because, in the source coe,filed expectedModCount of Itr given the value of modCount.They should be equal ,in theory
– Zhi Zheng
Nov 14 '18 at 14:20
@ZhiZheng they are equal when the iterator is created. If you perform structural modifications on theArrayList
while iterating over it (not including callingIterator
's remove method, which is allowed), themodCount
be be incremented and will no longer be equal to theIterator
'sexpectedModCount
. That's how theIterator
knows you did something wrong, which allows it to fail fast (by throwing ConcurrentModificationException).
– Eran
Nov 14 '18 at 14:23
Thank you very much,I think you are right.I will study it carefully again
– Zhi Zheng
Nov 14 '18 at 14:41
add a comment |
When I lookup where modCount
is located I can find it, it's located inside java.util.AbstractList
and is declared as:
protected transient int modCount = 0;
This means that it's initialized when it's declared.
Yes ,it is,thanks
– Zhi Zheng
Nov 14 '18 at 14:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301211%2fwhen-modcount-is-initialized-in-java-util-arraylist%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It is initialized to 0 (note that it's an instance variable of the AbstractList
super-class):
protected transient int modCount = 0;
and incremented in several places in which the List
is structurally modified (i.e. elements are added or removed).
For example:
public E remove(int index)
rangeCheck(index);
modCount++;
....
Note that the Itr
class is an inner class of ArrayList
, and therefore it has access to the members of the enclosing ArrayList
instance (which includes the modCount
instance variable of the AbstractList
super-class).
Thanks for your help.But I still have a doubt when I debugged,I found taht the expectedModCount instance varaible of the Itr class did not equal to the value of modCount instance variable of the ArrayList.Because, in the source coe,filed expectedModCount of Itr given the value of modCount.They should be equal ,in theory
– Zhi Zheng
Nov 14 '18 at 14:20
@ZhiZheng they are equal when the iterator is created. If you perform structural modifications on theArrayList
while iterating over it (not including callingIterator
's remove method, which is allowed), themodCount
be be incremented and will no longer be equal to theIterator
'sexpectedModCount
. That's how theIterator
knows you did something wrong, which allows it to fail fast (by throwing ConcurrentModificationException).
– Eran
Nov 14 '18 at 14:23
Thank you very much,I think you are right.I will study it carefully again
– Zhi Zheng
Nov 14 '18 at 14:41
add a comment |
It is initialized to 0 (note that it's an instance variable of the AbstractList
super-class):
protected transient int modCount = 0;
and incremented in several places in which the List
is structurally modified (i.e. elements are added or removed).
For example:
public E remove(int index)
rangeCheck(index);
modCount++;
....
Note that the Itr
class is an inner class of ArrayList
, and therefore it has access to the members of the enclosing ArrayList
instance (which includes the modCount
instance variable of the AbstractList
super-class).
Thanks for your help.But I still have a doubt when I debugged,I found taht the expectedModCount instance varaible of the Itr class did not equal to the value of modCount instance variable of the ArrayList.Because, in the source coe,filed expectedModCount of Itr given the value of modCount.They should be equal ,in theory
– Zhi Zheng
Nov 14 '18 at 14:20
@ZhiZheng they are equal when the iterator is created. If you perform structural modifications on theArrayList
while iterating over it (not including callingIterator
's remove method, which is allowed), themodCount
be be incremented and will no longer be equal to theIterator
'sexpectedModCount
. That's how theIterator
knows you did something wrong, which allows it to fail fast (by throwing ConcurrentModificationException).
– Eran
Nov 14 '18 at 14:23
Thank you very much,I think you are right.I will study it carefully again
– Zhi Zheng
Nov 14 '18 at 14:41
add a comment |
It is initialized to 0 (note that it's an instance variable of the AbstractList
super-class):
protected transient int modCount = 0;
and incremented in several places in which the List
is structurally modified (i.e. elements are added or removed).
For example:
public E remove(int index)
rangeCheck(index);
modCount++;
....
Note that the Itr
class is an inner class of ArrayList
, and therefore it has access to the members of the enclosing ArrayList
instance (which includes the modCount
instance variable of the AbstractList
super-class).
It is initialized to 0 (note that it's an instance variable of the AbstractList
super-class):
protected transient int modCount = 0;
and incremented in several places in which the List
is structurally modified (i.e. elements are added or removed).
For example:
public E remove(int index)
rangeCheck(index);
modCount++;
....
Note that the Itr
class is an inner class of ArrayList
, and therefore it has access to the members of the enclosing ArrayList
instance (which includes the modCount
instance variable of the AbstractList
super-class).
edited Nov 14 '18 at 13:29
answered Nov 14 '18 at 13:23
EranEran
288k37468558
288k37468558
Thanks for your help.But I still have a doubt when I debugged,I found taht the expectedModCount instance varaible of the Itr class did not equal to the value of modCount instance variable of the ArrayList.Because, in the source coe,filed expectedModCount of Itr given the value of modCount.They should be equal ,in theory
– Zhi Zheng
Nov 14 '18 at 14:20
@ZhiZheng they are equal when the iterator is created. If you perform structural modifications on theArrayList
while iterating over it (not including callingIterator
's remove method, which is allowed), themodCount
be be incremented and will no longer be equal to theIterator
'sexpectedModCount
. That's how theIterator
knows you did something wrong, which allows it to fail fast (by throwing ConcurrentModificationException).
– Eran
Nov 14 '18 at 14:23
Thank you very much,I think you are right.I will study it carefully again
– Zhi Zheng
Nov 14 '18 at 14:41
add a comment |
Thanks for your help.But I still have a doubt when I debugged,I found taht the expectedModCount instance varaible of the Itr class did not equal to the value of modCount instance variable of the ArrayList.Because, in the source coe,filed expectedModCount of Itr given the value of modCount.They should be equal ,in theory
– Zhi Zheng
Nov 14 '18 at 14:20
@ZhiZheng they are equal when the iterator is created. If you perform structural modifications on theArrayList
while iterating over it (not including callingIterator
's remove method, which is allowed), themodCount
be be incremented and will no longer be equal to theIterator
'sexpectedModCount
. That's how theIterator
knows you did something wrong, which allows it to fail fast (by throwing ConcurrentModificationException).
– Eran
Nov 14 '18 at 14:23
Thank you very much,I think you are right.I will study it carefully again
– Zhi Zheng
Nov 14 '18 at 14:41
Thanks for your help.But I still have a doubt when I debugged,I found taht the expectedModCount instance varaible of the Itr class did not equal to the value of modCount instance variable of the ArrayList.Because, in the source coe,filed expectedModCount of Itr given the value of modCount.They should be equal ,in theory
– Zhi Zheng
Nov 14 '18 at 14:20
Thanks for your help.But I still have a doubt when I debugged,I found taht the expectedModCount instance varaible of the Itr class did not equal to the value of modCount instance variable of the ArrayList.Because, in the source coe,filed expectedModCount of Itr given the value of modCount.They should be equal ,in theory
– Zhi Zheng
Nov 14 '18 at 14:20
@ZhiZheng they are equal when the iterator is created. If you perform structural modifications on the
ArrayList
while iterating over it (not including calling Iterator
's remove method, which is allowed), the modCount
be be incremented and will no longer be equal to the Iterator
's expectedModCount
. That's how the Iterator
knows you did something wrong, which allows it to fail fast (by throwing ConcurrentModificationException).– Eran
Nov 14 '18 at 14:23
@ZhiZheng they are equal when the iterator is created. If you perform structural modifications on the
ArrayList
while iterating over it (not including calling Iterator
's remove method, which is allowed), the modCount
be be incremented and will no longer be equal to the Iterator
's expectedModCount
. That's how the Iterator
knows you did something wrong, which allows it to fail fast (by throwing ConcurrentModificationException).– Eran
Nov 14 '18 at 14:23
Thank you very much,I think you are right.I will study it carefully again
– Zhi Zheng
Nov 14 '18 at 14:41
Thank you very much,I think you are right.I will study it carefully again
– Zhi Zheng
Nov 14 '18 at 14:41
add a comment |
When I lookup where modCount
is located I can find it, it's located inside java.util.AbstractList
and is declared as:
protected transient int modCount = 0;
This means that it's initialized when it's declared.
Yes ,it is,thanks
– Zhi Zheng
Nov 14 '18 at 14:42
add a comment |
When I lookup where modCount
is located I can find it, it's located inside java.util.AbstractList
and is declared as:
protected transient int modCount = 0;
This means that it's initialized when it's declared.
Yes ,it is,thanks
– Zhi Zheng
Nov 14 '18 at 14:42
add a comment |
When I lookup where modCount
is located I can find it, it's located inside java.util.AbstractList
and is declared as:
protected transient int modCount = 0;
This means that it's initialized when it's declared.
When I lookup where modCount
is located I can find it, it's located inside java.util.AbstractList
and is declared as:
protected transient int modCount = 0;
This means that it's initialized when it's declared.
answered Nov 14 '18 at 13:27
MarkMark
3,70921126
3,70921126
Yes ,it is,thanks
– Zhi Zheng
Nov 14 '18 at 14:42
add a comment |
Yes ,it is,thanks
– Zhi Zheng
Nov 14 '18 at 14:42
Yes ,it is,thanks
– Zhi Zheng
Nov 14 '18 at 14:42
Yes ,it is,thanks
– Zhi Zheng
Nov 14 '18 at 14:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301211%2fwhen-modcount-is-initialized-in-java-util-arraylist%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown