when modCount is initialized in java.util.ArrayList?










2















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



enter image description here



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.



enter image description here










share|improve this question


























    2















    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



    enter image description here



    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.



    enter image description here










    share|improve this question
























      2












      2








      2








      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



      enter image description here



      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.



      enter image description here










      share|improve this question














      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



      enter image description here



      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.



      enter image description here







      java iterator






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 13:20









      Zhi ZhengZhi Zheng

      414




      414






















          2 Answers
          2






          active

          oldest

          votes


















          2














          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).






          share|improve this answer

























          • 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











          • Thank you very much,I think you are right.I will study it carefully again

            – Zhi Zheng
            Nov 14 '18 at 14:41


















          0














          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.






          share|improve this answer























          • Yes ,it is,thanks

            – Zhi Zheng
            Nov 14 '18 at 14:42










          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
          );



          );













          draft saved

          draft discarded


















          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









          2














          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).






          share|improve this answer

























          • 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











          • Thank you very much,I think you are right.I will study it carefully again

            – Zhi Zheng
            Nov 14 '18 at 14:41















          2














          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).






          share|improve this answer

























          • 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











          • Thank you very much,I think you are right.I will study it carefully again

            – Zhi Zheng
            Nov 14 '18 at 14:41













          2












          2








          2







          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).






          share|improve this answer















          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).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 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

















          • 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











          • 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













          0














          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.






          share|improve this answer























          • Yes ,it is,thanks

            – Zhi Zheng
            Nov 14 '18 at 14:42















          0














          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.






          share|improve this answer























          • Yes ,it is,thanks

            – Zhi Zheng
            Nov 14 '18 at 14:42













          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 13:27









          MarkMark

          3,70921126




          3,70921126












          • 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





          Yes ,it is,thanks

          – Zhi Zheng
          Nov 14 '18 at 14:42

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo