BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ArrayList ensureCapacity() Method example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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

Top Related Articles:

  1. Java StringBuilder append() Method
  2. Java StringBuffer toString()
  3. Java StringBuilder substring()
  4. Java StringBuffer capacity()
  5. Perform Binary Search on ArrayList in Java

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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 *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap