beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java ArrayList ensureCapacity() Method example

By Chaitanya Singh | Filed Under: Java Collections

ArrayList internally implements growable dynamic array which means it can increase and decrease its size automatically. If we try to add an element to a already full ArrayList then it automatically re-sized internally to accommodate the new element however sometimes its not a good approach.

Consider a scenario when there is a need to add huge number of elements to an already full ArrayList, in such case ArrayList has to be resized several number of times which would result in a poor performance. For such scenarios ensureCapacity() method of Java.util.ArrayList class is very useful as it increases the size of the ArrayList by a specified capacity.

public void ensureCapacity(int minCapacity)

Example

package beginnersbook.com;
import java.util.ArrayList;
public class EnsureCapacityExample {
  public static void main(String args[]) {
      // ArrayList with Capacity 4
      ArrayList<String> al = new ArrayList<String>(4);
      //Added 4 elements
      al.add("Hi");
      al.add("Hello");
      al.add("Bye");
      al.add("GM");

      //Increase capacity to 5
      al.ensureCapacity(5);

      al.add("GE");
      // let us print all the elements available in list
      for (String temp: al) {
            System.out.println(temp);
      }
   }
}

Output:

Hi
Hello
Bye
GM
GE

Enjoyed this post? Try these related posts

  1. How to get sublist of an ArrayList with example
  2. Map.Entry Interface in Java
  3. Java ArrayList of Object Sort Example (Comparable And Comparator)
  4. How to iterate TreeMap in reverse order in Java
  5. How to convert LinkedList to array using toArray() in Java
  6. Java – Remove all mappings from HashMap example

Comments

  1. Ray says

    January 11, 2016 at 8:46 PM

    Thank you for those easy understanding examples. Is there a typo in al.ensureCapacity(55), (where 55 should be 5 according to the comment)?

    Reply
  2. Abir Nandy says

    August 22, 2018 at 6:22 PM

    It’s not working in my code where I am checking the size of the arraylist after ensuring the capacity.
    The code is as follows:-
    import java.io.*;
    import java.util.*;

    class Exp
    {
    public static void main(String args[])
    {
    int n,i;
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    ArrayList in=new ArrayList();
    in.ensureCapacity(n);
    System.out.println(in.size());
    }
    }

    Reply
  3. Abir Nandy says

    August 22, 2018 at 6:25 PM

    When an arraylist is having an element repeated than what will get returned if indexOf() that element has been used?

    Reply
    • Chaitanya Singh says

      September 9, 2018 at 1:32 PM

      It will return the index of the first occurrence of the element in the list

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

ArrayList Tutorial

  • Java ArrayList

Java ArrayList Methods

  • add(Object obj)
  • add(int index, Object element)
  • addAll(Collection c)
  • addAll(int index, Collection c)
  • contains()
  • get()
  • indexOf()
  • ensureCapacity()
  • isEmpty()
  • lastIndexOf()
  • remove()
  • remove(Object obj)
  • trimToSize()
  • set()
  • clone()
  • clear()
  • size()

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap