ArrayList public constructor - “Constructs an empty list with an initial capacity of ten” - where?...












7
















This question already has an answer here:




  • In Java 8, why is the default capacity of ArrayList now zero?

    6 answers




Be aware, it is not a duplicate of Why start an ArrayList with an initial capacity?



Looking into the source code of the java.util.ArrayList class, starting from at least java 1.8 I see the following code:



/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}


Where



private static final Object DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};


Though the javadoc officially states:




Constructs an empty list with an initial capacity of ten.




I outline: ...an initial capacity of ten. Where is this ten?



Am I completely mad and missing something, or there is simply a javadoc bug here?



UPD: How it looked like prior java 1.8:



public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this(10);
}









share|improve this question















marked as duplicate by Aomine java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 13 '18 at 13:58


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    7
















    This question already has an answer here:




    • In Java 8, why is the default capacity of ArrayList now zero?

      6 answers




    Be aware, it is not a duplicate of Why start an ArrayList with an initial capacity?



    Looking into the source code of the java.util.ArrayList class, starting from at least java 1.8 I see the following code:



    /**
    * Constructs an empty list with an initial capacity of ten.
    */
    public ArrayList() {
    this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }


    Where



    private static final Object DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};


    Though the javadoc officially states:




    Constructs an empty list with an initial capacity of ten.




    I outline: ...an initial capacity of ten. Where is this ten?



    Am I completely mad and missing something, or there is simply a javadoc bug here?



    UPD: How it looked like prior java 1.8:



    public ArrayList(int initialCapacity) {
    super();
    if (initialCapacity < 0)
    throw new IllegalArgumentException("Illegal Capacity: "+
    initialCapacity);
    this.elementData = new Object[initialCapacity];
    }

    /**
    * Constructs an empty list with an initial capacity of ten.
    */
    public ArrayList() {
    this(10);
    }









    share|improve this question















    marked as duplicate by Aomine java
    Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Dec 13 '18 at 13:58


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      7












      7








      7


      1







      This question already has an answer here:




      • In Java 8, why is the default capacity of ArrayList now zero?

        6 answers




      Be aware, it is not a duplicate of Why start an ArrayList with an initial capacity?



      Looking into the source code of the java.util.ArrayList class, starting from at least java 1.8 I see the following code:



      /**
      * Constructs an empty list with an initial capacity of ten.
      */
      public ArrayList() {
      this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
      }


      Where



      private static final Object DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};


      Though the javadoc officially states:




      Constructs an empty list with an initial capacity of ten.




      I outline: ...an initial capacity of ten. Where is this ten?



      Am I completely mad and missing something, or there is simply a javadoc bug here?



      UPD: How it looked like prior java 1.8:



      public ArrayList(int initialCapacity) {
      super();
      if (initialCapacity < 0)
      throw new IllegalArgumentException("Illegal Capacity: "+
      initialCapacity);
      this.elementData = new Object[initialCapacity];
      }

      /**
      * Constructs an empty list with an initial capacity of ten.
      */
      public ArrayList() {
      this(10);
      }









      share|improve this question

















      This question already has an answer here:




      • In Java 8, why is the default capacity of ArrayList now zero?

        6 answers




      Be aware, it is not a duplicate of Why start an ArrayList with an initial capacity?



      Looking into the source code of the java.util.ArrayList class, starting from at least java 1.8 I see the following code:



      /**
      * Constructs an empty list with an initial capacity of ten.
      */
      public ArrayList() {
      this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
      }


      Where



      private static final Object DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};


      Though the javadoc officially states:




      Constructs an empty list with an initial capacity of ten.




      I outline: ...an initial capacity of ten. Where is this ten?



      Am I completely mad and missing something, or there is simply a javadoc bug here?



      UPD: How it looked like prior java 1.8:



      public ArrayList(int initialCapacity) {
      super();
      if (initialCapacity < 0)
      throw new IllegalArgumentException("Illegal Capacity: "+
      initialCapacity);
      this.elementData = new Object[initialCapacity];
      }

      /**
      * Constructs an empty list with an initial capacity of ten.
      */
      public ArrayList() {
      this(10);
      }




      This question already has an answer here:




      • In Java 8, why is the default capacity of ArrayList now zero?

        6 answers








      java arraylist






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 26 '18 at 20:11









      Andy Thomas

      67.4k979131




      67.4k979131










      asked Dec 13 '18 at 13:47









      AndremoniyAndremoniy

      22k675161




      22k675161




      marked as duplicate by Aomine java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 13 '18 at 13:58


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Aomine java
      Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Dec 13 '18 at 13:58


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          7














          This is an optimization. The developers decided to initialize the ArrayList with an empty backing array, and lazily create a non-empty backing array only when you start adding elements to the List.



          When you add the first element (by calling add), it calls



          ensureCapacityInternal(size + 1);


          which checks if elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA, and if so, sets the capacity to



          minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);


          DEFAULT_CAPACITY is 10.






          share|improve this answer


























          • But I still don't get, it is explicitly said: an initial capacity of ten. Where is this ten? :p

            – Andremoniy
            Dec 13 '18 at 13:51











          • @Andremoniy see edit

            – Eran
            Dec 13 '18 at 13:52











          • I see. It's a good catch. No doubts. HOWEVER. There is a big difference for those who only reads the javadoc: before java 1.8 it would make sense to create ArrayList(0) to make it a slightly more efficient, isn't it? So I would insist that this is a kind of bug in javadoc...

            – Andremoniy
            Dec 13 '18 at 13:54






          • 1





            @Andremoniy I'm not sure it's a bug in javadoc. I'd call it an implementation detail.

            – Eran
            Dec 13 '18 at 13:56






          • 1





            @Andremoniy add calls ensureCapacityInternal(1) (when the list is empty), which calls ensureExplicitCapacity(10), which calls grow(10), which calls elementData = Arrays.copyOf(elementData, newCapacity);

            – Eran
            Dec 13 '18 at 14:00



















          3














          The capacity will be set as 10 when you add element to the list first time.



          See this:



          /**
          * Default initial capacity.
          */
          private static final int DEFAULT_CAPACITY = 10;


          Complete procedure



          Step 1:



          public boolean add(E e) {
          modCount++;
          add(e, elementData, size);
          return true;
          }


          Step 2:



          private void add(E e, Object elementData, int s) {
          if (s == elementData.length)
          elementData = grow();
          elementData[s] = e;
          size = s + 1;
          }


          Step 3, the list grows:



          private Object grow() {
          return grow(size + 1); // size is 0 here
          }

          private Object grow(int minCapacity) {
          return elementData = Arrays.copyOf(elementData,
          newCapacity(minCapacity)); // newCapacity(1) will return 10, see step 4, the elementData will have capacity 10.
          }


          Step 4, call newCapacity(1):



          private int newCapacity(int minCapacity) {
          // overflow-conscious code
          int oldCapacity = elementData.length;
          int newCapacity = oldCapacity + (oldCapacity >> 1);
          if (newCapacity - minCapacity <= 0) {
          if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
          return Math.max(DEFAULT_CAPACITY, minCapacity); // will return 10 here !!!!
          if (minCapacity < 0) // overflow
          throw new OutOfMemoryError();
          return minCapacity;
          }
          return (newCapacity - MAX_ARRAY_SIZE <= 0)
          ? newCapacity
          : hugeCapacity(minCapacity);
          }





          share|improve this answer


























          • Appreciate your detailed answer. Though it appears that my question is a duplicate.

            – Andremoniy
            Dec 13 '18 at 14:07


















          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          7














          This is an optimization. The developers decided to initialize the ArrayList with an empty backing array, and lazily create a non-empty backing array only when you start adding elements to the List.



          When you add the first element (by calling add), it calls



          ensureCapacityInternal(size + 1);


          which checks if elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA, and if so, sets the capacity to



          minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);


          DEFAULT_CAPACITY is 10.






          share|improve this answer


























          • But I still don't get, it is explicitly said: an initial capacity of ten. Where is this ten? :p

            – Andremoniy
            Dec 13 '18 at 13:51











          • @Andremoniy see edit

            – Eran
            Dec 13 '18 at 13:52











          • I see. It's a good catch. No doubts. HOWEVER. There is a big difference for those who only reads the javadoc: before java 1.8 it would make sense to create ArrayList(0) to make it a slightly more efficient, isn't it? So I would insist that this is a kind of bug in javadoc...

            – Andremoniy
            Dec 13 '18 at 13:54






          • 1





            @Andremoniy I'm not sure it's a bug in javadoc. I'd call it an implementation detail.

            – Eran
            Dec 13 '18 at 13:56






          • 1





            @Andremoniy add calls ensureCapacityInternal(1) (when the list is empty), which calls ensureExplicitCapacity(10), which calls grow(10), which calls elementData = Arrays.copyOf(elementData, newCapacity);

            – Eran
            Dec 13 '18 at 14:00
















          7














          This is an optimization. The developers decided to initialize the ArrayList with an empty backing array, and lazily create a non-empty backing array only when you start adding elements to the List.



          When you add the first element (by calling add), it calls



          ensureCapacityInternal(size + 1);


          which checks if elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA, and if so, sets the capacity to



          minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);


          DEFAULT_CAPACITY is 10.






          share|improve this answer


























          • But I still don't get, it is explicitly said: an initial capacity of ten. Where is this ten? :p

            – Andremoniy
            Dec 13 '18 at 13:51











          • @Andremoniy see edit

            – Eran
            Dec 13 '18 at 13:52











          • I see. It's a good catch. No doubts. HOWEVER. There is a big difference for those who only reads the javadoc: before java 1.8 it would make sense to create ArrayList(0) to make it a slightly more efficient, isn't it? So I would insist that this is a kind of bug in javadoc...

            – Andremoniy
            Dec 13 '18 at 13:54






          • 1





            @Andremoniy I'm not sure it's a bug in javadoc. I'd call it an implementation detail.

            – Eran
            Dec 13 '18 at 13:56






          • 1





            @Andremoniy add calls ensureCapacityInternal(1) (when the list is empty), which calls ensureExplicitCapacity(10), which calls grow(10), which calls elementData = Arrays.copyOf(elementData, newCapacity);

            – Eran
            Dec 13 '18 at 14:00














          7












          7








          7







          This is an optimization. The developers decided to initialize the ArrayList with an empty backing array, and lazily create a non-empty backing array only when you start adding elements to the List.



          When you add the first element (by calling add), it calls



          ensureCapacityInternal(size + 1);


          which checks if elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA, and if so, sets the capacity to



          minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);


          DEFAULT_CAPACITY is 10.






          share|improve this answer















          This is an optimization. The developers decided to initialize the ArrayList with an empty backing array, and lazily create a non-empty backing array only when you start adding elements to the List.



          When you add the first element (by calling add), it calls



          ensureCapacityInternal(size + 1);


          which checks if elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA, and if so, sets the capacity to



          minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);


          DEFAULT_CAPACITY is 10.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 13 '18 at 14:05

























          answered Dec 13 '18 at 13:49









          EranEran

          285k37463551




          285k37463551













          • But I still don't get, it is explicitly said: an initial capacity of ten. Where is this ten? :p

            – Andremoniy
            Dec 13 '18 at 13:51











          • @Andremoniy see edit

            – Eran
            Dec 13 '18 at 13:52











          • I see. It's a good catch. No doubts. HOWEVER. There is a big difference for those who only reads the javadoc: before java 1.8 it would make sense to create ArrayList(0) to make it a slightly more efficient, isn't it? So I would insist that this is a kind of bug in javadoc...

            – Andremoniy
            Dec 13 '18 at 13:54






          • 1





            @Andremoniy I'm not sure it's a bug in javadoc. I'd call it an implementation detail.

            – Eran
            Dec 13 '18 at 13:56






          • 1





            @Andremoniy add calls ensureCapacityInternal(1) (when the list is empty), which calls ensureExplicitCapacity(10), which calls grow(10), which calls elementData = Arrays.copyOf(elementData, newCapacity);

            – Eran
            Dec 13 '18 at 14:00



















          • But I still don't get, it is explicitly said: an initial capacity of ten. Where is this ten? :p

            – Andremoniy
            Dec 13 '18 at 13:51











          • @Andremoniy see edit

            – Eran
            Dec 13 '18 at 13:52











          • I see. It's a good catch. No doubts. HOWEVER. There is a big difference for those who only reads the javadoc: before java 1.8 it would make sense to create ArrayList(0) to make it a slightly more efficient, isn't it? So I would insist that this is a kind of bug in javadoc...

            – Andremoniy
            Dec 13 '18 at 13:54






          • 1





            @Andremoniy I'm not sure it's a bug in javadoc. I'd call it an implementation detail.

            – Eran
            Dec 13 '18 at 13:56






          • 1





            @Andremoniy add calls ensureCapacityInternal(1) (when the list is empty), which calls ensureExplicitCapacity(10), which calls grow(10), which calls elementData = Arrays.copyOf(elementData, newCapacity);

            – Eran
            Dec 13 '18 at 14:00

















          But I still don't get, it is explicitly said: an initial capacity of ten. Where is this ten? :p

          – Andremoniy
          Dec 13 '18 at 13:51





          But I still don't get, it is explicitly said: an initial capacity of ten. Where is this ten? :p

          – Andremoniy
          Dec 13 '18 at 13:51













          @Andremoniy see edit

          – Eran
          Dec 13 '18 at 13:52





          @Andremoniy see edit

          – Eran
          Dec 13 '18 at 13:52













          I see. It's a good catch. No doubts. HOWEVER. There is a big difference for those who only reads the javadoc: before java 1.8 it would make sense to create ArrayList(0) to make it a slightly more efficient, isn't it? So I would insist that this is a kind of bug in javadoc...

          – Andremoniy
          Dec 13 '18 at 13:54





          I see. It's a good catch. No doubts. HOWEVER. There is a big difference for those who only reads the javadoc: before java 1.8 it would make sense to create ArrayList(0) to make it a slightly more efficient, isn't it? So I would insist that this is a kind of bug in javadoc...

          – Andremoniy
          Dec 13 '18 at 13:54




          1




          1





          @Andremoniy I'm not sure it's a bug in javadoc. I'd call it an implementation detail.

          – Eran
          Dec 13 '18 at 13:56





          @Andremoniy I'm not sure it's a bug in javadoc. I'd call it an implementation detail.

          – Eran
          Dec 13 '18 at 13:56




          1




          1





          @Andremoniy add calls ensureCapacityInternal(1) (when the list is empty), which calls ensureExplicitCapacity(10), which calls grow(10), which calls elementData = Arrays.copyOf(elementData, newCapacity);

          – Eran
          Dec 13 '18 at 14:00





          @Andremoniy add calls ensureCapacityInternal(1) (when the list is empty), which calls ensureExplicitCapacity(10), which calls grow(10), which calls elementData = Arrays.copyOf(elementData, newCapacity);

          – Eran
          Dec 13 '18 at 14:00













          3














          The capacity will be set as 10 when you add element to the list first time.



          See this:



          /**
          * Default initial capacity.
          */
          private static final int DEFAULT_CAPACITY = 10;


          Complete procedure



          Step 1:



          public boolean add(E e) {
          modCount++;
          add(e, elementData, size);
          return true;
          }


          Step 2:



          private void add(E e, Object elementData, int s) {
          if (s == elementData.length)
          elementData = grow();
          elementData[s] = e;
          size = s + 1;
          }


          Step 3, the list grows:



          private Object grow() {
          return grow(size + 1); // size is 0 here
          }

          private Object grow(int minCapacity) {
          return elementData = Arrays.copyOf(elementData,
          newCapacity(minCapacity)); // newCapacity(1) will return 10, see step 4, the elementData will have capacity 10.
          }


          Step 4, call newCapacity(1):



          private int newCapacity(int minCapacity) {
          // overflow-conscious code
          int oldCapacity = elementData.length;
          int newCapacity = oldCapacity + (oldCapacity >> 1);
          if (newCapacity - minCapacity <= 0) {
          if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
          return Math.max(DEFAULT_CAPACITY, minCapacity); // will return 10 here !!!!
          if (minCapacity < 0) // overflow
          throw new OutOfMemoryError();
          return minCapacity;
          }
          return (newCapacity - MAX_ARRAY_SIZE <= 0)
          ? newCapacity
          : hugeCapacity(minCapacity);
          }





          share|improve this answer


























          • Appreciate your detailed answer. Though it appears that my question is a duplicate.

            – Andremoniy
            Dec 13 '18 at 14:07
















          3














          The capacity will be set as 10 when you add element to the list first time.



          See this:



          /**
          * Default initial capacity.
          */
          private static final int DEFAULT_CAPACITY = 10;


          Complete procedure



          Step 1:



          public boolean add(E e) {
          modCount++;
          add(e, elementData, size);
          return true;
          }


          Step 2:



          private void add(E e, Object elementData, int s) {
          if (s == elementData.length)
          elementData = grow();
          elementData[s] = e;
          size = s + 1;
          }


          Step 3, the list grows:



          private Object grow() {
          return grow(size + 1); // size is 0 here
          }

          private Object grow(int minCapacity) {
          return elementData = Arrays.copyOf(elementData,
          newCapacity(minCapacity)); // newCapacity(1) will return 10, see step 4, the elementData will have capacity 10.
          }


          Step 4, call newCapacity(1):



          private int newCapacity(int minCapacity) {
          // overflow-conscious code
          int oldCapacity = elementData.length;
          int newCapacity = oldCapacity + (oldCapacity >> 1);
          if (newCapacity - minCapacity <= 0) {
          if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
          return Math.max(DEFAULT_CAPACITY, minCapacity); // will return 10 here !!!!
          if (minCapacity < 0) // overflow
          throw new OutOfMemoryError();
          return minCapacity;
          }
          return (newCapacity - MAX_ARRAY_SIZE <= 0)
          ? newCapacity
          : hugeCapacity(minCapacity);
          }





          share|improve this answer


























          • Appreciate your detailed answer. Though it appears that my question is a duplicate.

            – Andremoniy
            Dec 13 '18 at 14:07














          3












          3








          3







          The capacity will be set as 10 when you add element to the list first time.



          See this:



          /**
          * Default initial capacity.
          */
          private static final int DEFAULT_CAPACITY = 10;


          Complete procedure



          Step 1:



          public boolean add(E e) {
          modCount++;
          add(e, elementData, size);
          return true;
          }


          Step 2:



          private void add(E e, Object elementData, int s) {
          if (s == elementData.length)
          elementData = grow();
          elementData[s] = e;
          size = s + 1;
          }


          Step 3, the list grows:



          private Object grow() {
          return grow(size + 1); // size is 0 here
          }

          private Object grow(int minCapacity) {
          return elementData = Arrays.copyOf(elementData,
          newCapacity(minCapacity)); // newCapacity(1) will return 10, see step 4, the elementData will have capacity 10.
          }


          Step 4, call newCapacity(1):



          private int newCapacity(int minCapacity) {
          // overflow-conscious code
          int oldCapacity = elementData.length;
          int newCapacity = oldCapacity + (oldCapacity >> 1);
          if (newCapacity - minCapacity <= 0) {
          if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
          return Math.max(DEFAULT_CAPACITY, minCapacity); // will return 10 here !!!!
          if (minCapacity < 0) // overflow
          throw new OutOfMemoryError();
          return minCapacity;
          }
          return (newCapacity - MAX_ARRAY_SIZE <= 0)
          ? newCapacity
          : hugeCapacity(minCapacity);
          }





          share|improve this answer















          The capacity will be set as 10 when you add element to the list first time.



          See this:



          /**
          * Default initial capacity.
          */
          private static final int DEFAULT_CAPACITY = 10;


          Complete procedure



          Step 1:



          public boolean add(E e) {
          modCount++;
          add(e, elementData, size);
          return true;
          }


          Step 2:



          private void add(E e, Object elementData, int s) {
          if (s == elementData.length)
          elementData = grow();
          elementData[s] = e;
          size = s + 1;
          }


          Step 3, the list grows:



          private Object grow() {
          return grow(size + 1); // size is 0 here
          }

          private Object grow(int minCapacity) {
          return elementData = Arrays.copyOf(elementData,
          newCapacity(minCapacity)); // newCapacity(1) will return 10, see step 4, the elementData will have capacity 10.
          }


          Step 4, call newCapacity(1):



          private int newCapacity(int minCapacity) {
          // overflow-conscious code
          int oldCapacity = elementData.length;
          int newCapacity = oldCapacity + (oldCapacity >> 1);
          if (newCapacity - minCapacity <= 0) {
          if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
          return Math.max(DEFAULT_CAPACITY, minCapacity); // will return 10 here !!!!
          if (minCapacity < 0) // overflow
          throw new OutOfMemoryError();
          return minCapacity;
          }
          return (newCapacity - MAX_ARRAY_SIZE <= 0)
          ? newCapacity
          : hugeCapacity(minCapacity);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 13 '18 at 14:08

























          answered Dec 13 '18 at 13:53









          孙兴斌孙兴斌

          17.8k41752




          17.8k41752













          • Appreciate your detailed answer. Though it appears that my question is a duplicate.

            – Andremoniy
            Dec 13 '18 at 14:07



















          • Appreciate your detailed answer. Though it appears that my question is a duplicate.

            – Andremoniy
            Dec 13 '18 at 14:07

















          Appreciate your detailed answer. Though it appears that my question is a duplicate.

          – Andremoniy
          Dec 13 '18 at 14:07





          Appreciate your detailed answer. Though it appears that my question is a duplicate.

          – Andremoniy
          Dec 13 '18 at 14:07



          Popular posts from this blog

          Probability when a professor distributes a quiz and homework assignment to a class of n students.

          Aardman Animations

          Are they similar matrix