Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an element to ArrayList when there is no space left [duplicate]

Tags:

java

arraylist

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?

like image 884
Omar ZRIDI Avatar asked Apr 14 '16 09:04

Omar ZRIDI


People also ask

How do you add to an ArrayList without duplicates?

The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList : Set<String> set = new HashSet<>(yourList); yourList. clear(); yourList. addAll(set);

Can ArrayList hold duplicates?

ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.

How do you check if there are duplicates in an ArrayList Java?

One more way to detect duplication in the java array is adding every element of the array into HashSet which is a Set implementation. Since the add(Object obj) method of Set returns false if Set already contains an element to be added, it can be used to find out if the array contains duplicates in Java or not.

What does .ADD do in Java ArrayList?

The java. util. ArrayList. add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).


1 Answers

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.

like image 107
Am_I_Helpful Avatar answered Oct 16 '22 07:10

Am_I_Helpful