5

I'm trying to understand add operation of ArrayList Class in Java from here. Here is a portion of code:

//Proprties:

  107       /**
  108        * The array buffer into which the elements of the ArrayList are stored.
  109        * The capacity of the ArrayList is the length of this array buffer.
  110        */
  111       private transient Object[] elementData;
  112   
  113       /**
  114        * The size of the ArrayList (the number of elements it contains).
  115        *
  116        * @serial
  117        */
  118       private int size;


  404       /**
  405        * Appends the specified element to the end of this list.
  406        *
  407        * @param e element to be appended to this list
  408        * @return <tt>true</tt> (as specified by {@link Collection#add})
  409        */
  410       public boolean add(E e) {
  411           ensureCapacityInternal(size + 1);  // Increments modCount!!
  412           elementData[size++] = e;
  413           return true;
  414       }

  183       private void ensureCapacityInternal(int minCapacity) {
  184           modCount++;
  185           // overflow-conscious code
  186           if (minCapacity - elementData.length > 0)
  187               grow(minCapacity);
  188       }

  198       /**
  199        * Increases the capacity to ensure that it can hold at least the
  200        * number of elements specified by the minimum capacity argument.
  201        *
  202        * @param minCapacity the desired minimum capacity
  203        */
  204       private void grow(int minCapacity) {
  205           // overflow-conscious code
  206           int oldCapacity = elementData.length;
  207           int newCapacity = oldCapacity + (oldCapacity >> 1);
  208           if (newCapacity - minCapacity < 0)
  209               newCapacity = minCapacity;
  210           if (newCapacity - MAX_ARRAY_SIZE > 0)
  211               newCapacity = hugeCapacity(minCapacity);
  212           // minCapacity is usually close to size, so this is a win:
  213           elementData = Arrays.copyOf(elementData, newCapacity);
  214       }

Steps for the elements insertion are: verify if it does exist enough space before inserting a new element by calling ensureCapacityInternal , in case there is no enough space we call grow operation to increase elementData capacity.

My problem is that I can't see a case where newCapacity - minCapacity < 0 condition in grow operation will satisfied.

Can you explain to me the utility of this condition or give an example where this condition would be satisfied?

Zrom
  • 1,192
  • 11
  • 24
  • 4
    This was already asked here - http://stackoverflow.com/questions/33147339/difference-between-if-a-b-0-and-if-a-b – Eran Apr 14 '16 at 09:06
  • @Eran Yes it's the same question asked in another way. Thanks – Zrom Apr 14 '16 at 20:44

2 Answers2

5

My problem is that I can't see a case where newCapacity - minCapacity < 0 condition in grow operation will satisfied.

Can you explain to me the utility of this condition or give an example where this condition would be satisfied?

What about the case when integer overflow occurs? This is the best case, which depicts that ArrayList cannot grow any further.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
3

The case when minCapacity is bigger than newCapacity is not in the case of add but when you use the addAll method. There you can provide a Collection of elements which can be for example 100 new Objects to insert onto your empty ArrayList. In this case the default capacity of 10 will be used to calculate the new capacity:

int newCapacity = oldCapacity + (oldCapacity >> 1); (10 + 5 = 15)

In this case 15 is not enough to store 100 elements. In this case if (newCapacity - minCapacity < 0) is evaluated to true.

GHajba
  • 3,665
  • 5
  • 25
  • 35
  • Thank you for the response. You're right . But in my case it's integer overflow problem – Zrom Apr 14 '16 at 21:59