Java ArrayList ensureCapacity() method with example

Java ArrayList ensureCapacity() method Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Method Signature

public void ensureCapacity(int minCapacity)

Parameters:

minCapacity – the desired minimum capacity

ArrayList ensureCapacity() method implementation

public void ensureCapacity(int minCapacity) {
        int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
            // any size if not default element table
            ? 0
            // larger than default for default empty table. It's already
            // supposed to be at default size.
            : DEFAULT_CAPACITY;

        if (minCapacity > minExpand) {
            ensureExplicitCapacity(minCapacity);
        }
    }
private void ensureExplicitCapacity(int minCapacity) {
        modCount++;

        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
 private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        // minCapacity is usually close to size, so this is a win:
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

Java ArrayList ensureCapacity() method example

package com.ehowtonow.java.util.arraylist;

import java.util.ArrayList;

public class EnsureCapacityExample {

	public static void main(String[] args) {

		// create empty array list with Integer type
		ArrayList<Integer> numericList = new ArrayList<Integer>(2);

		// add() method to add Integer element in the list
		numericList.add(10);
		numericList.add(15);
		numericList.add(20);
		numericList.add(25);
		numericList.add(30);

		/*
		 * ensure that the ArrayList can hold upto 20 elements using ensureCapacity()
		 * method. If don't have enough capacity will increase the capacity of the
		 * ArrayList to 20 elements
		 */
		numericList.ensureCapacity(20);
		
		for (Integer numbers : numericList) {
			System.out.println(" Numbers : "+numbers);
		}
	}

}

Output

Numbers : 10
Numbers : 15
Numbers : 20
Numbers : 25
Numbers : 30


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java ArrayList add(E e) method with example
  2. Java ArrayList contains() method with example
  3. Java ArrayList remove(Object o) method with example
  4. Java ArrayList add(int index, E element) method with example
  5. Java ArrayList clear() method with example
  6. Java ArrayList addAll(Collection< ? extends E> c) method with example
  7. Java ArrayList clone() method with example
  8. Java ArrayList indexOf() method with example
  9. Java ArrayList forEach() method with example
  10. Java ArrayList remove() method with example
  11. Java ArrayList get() method with example
  12. Java ArrayList isEmpty() method with example
  13. Java ArrayList addAll(int index, Collection c) method with example
  14. Java ArrayList ensureCapacity() method with example
  15. Java ArrayList removeAll() method with example

Be the first to comment

Leave a Reply