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

How to Increase the capacity of ArrayList

By Chaitanya Singh | Filed Under: java

When we create an ArrayList in Java, it is created with a default capacity of 10. However an ArrayList can be automatically resized if more elements are added than the initial capacity of ArrayList. This is useful, as you do not need to worry about the size of ArrayList. However if you are sure about the minimum number of elements that needs to be added to the ArrayList, you can specify that using the ensureCapacity() method.

Why we need to increase the size of ArrayList?

You may be wondering, why we need to increase the size of ArrayList, if it is already resized whenever needed. The reason is simple, to avoid the performance issues due to continuous resizing of an ArrayList.

For example: you are sure that you need to add at-least 1000 elements to an ArrayList, you can specify that number using ensureCapacity(), however if you do not specify this, the ArrayList needs to be resized multiple number of times. By specifying the capacity in the beginning you ensure the better performance for the add operation on ArrayList.

How to Increase the capacity (Size) of ArrayList?

In the following program, we have created an ArrayList with the initial capacity of 5. We want to store minimum 1000 elements to this ArrayList. For this, we are using ensureCapacity(1000). We have placed this method inside try-catch block to handle any exception. If the program throws any exception, print the exception else print a successful message “ArrayList can now store upto 1000 elements”.

import java.util.ArrayList;
public class JavaExample {
  public static void main(String[] arg) throws Exception
  {
    try {
      // Creating an ArrayList with the initial capacity of 5
      ArrayList<String> city = new ArrayList<String>(5);

      // Adding elements to the ArrayList
      city.add("Pune");
      city.add("Noida");
      city.add("Agra");
      city.add("Delhi");
      city.add("Chennai");

      // Print the ArrayList
      System.out.println("ArrayList: " + city);


      System.out.println("Increasing the capacity to 1000.");

      // Increasing the capacity of ArrayList to 1000
      city.ensureCapacity(1000);

      //If execution reached at this statement, it means no exception
      //is thrown and the increase capcity operation is successful.
      System.out.println("ArrayList can now store upto 1000 elements.");
    }

    catch (NullPointerException e) {
      System.out.println("Cannot increase capacity: " + e);
    }
  }
}

Output:

ArrayList: [Pune, Noida, Agra, Delhi, Chennai]
Increasing the capacity to 1000.
ArrayList can now store upto 1000 elements.

Recommended Articles:

  • Add multiple items to an ArrayList
  • Get last element of an ArrayList
  • Get sublist of an ArrayList
❮ Java Collections

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 – 2022 BeginnersBook . Privacy Policy . Sitemap