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?