The ensureCapacity() method of java.util.ArrayList class 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.
Syntax:
public void ensureCapacity(int minCapacity)
Parameters: This method takes the desired minimum capacity as a parameter.
Below are the examples to illustrate the ensureCapacity() method.
Example 1:
// Java program to demonstrate // ensureCapacity() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of ArrayList<Integer> ArrayList<Integer> arrlist = new ArrayList<Integer>(); // adding element to arrlist arrlist.add(10); arrlist.add(20); arrlist.add(30); arrlist.add(40); // Print the ArrayList System.out.println("ArrayList: " + arrlist); // ensure that the ArrayList // can hold upto 5000 elements // using ensureCapacity() method arrlist.ensureCapacity(5000); // Print System.out.println("ArrayList can now" + " surely store upto" + " 5000 elements."); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
ArrayList: [10, 20, 30, 40] ArrayList can now surely store upto 5000 elements.
Example 2:
// Java program to demonstrate // ensureCapacity() method for String value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // Creating object of ArrayList<Integer> ArrayList<String> arrlist = new ArrayList<String>(); // adding element to arrlist arrlist.add("A"); arrlist.add("B"); arrlist.add("C"); arrlist.add("D"); // Print the ArrayList System.out.println("ArrayList: " + arrlist); // ensure that the ArrayList // can hold upto 400 elements // using ensureCapacity() method arrlist.ensureCapacity(400); // Print System.out.println("ArrayList can now" + " surely store upto" + " 400 elements."); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
ArrayList: [A, B, C, D] ArrayList can now surely store upto 400 elements.
Recommended Posts:
- StringBuffer ensureCapacity() method in Java with Examples
- StringBuilder ensureCapacity() in Java with Examples
- ArrayList set() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- ArrayList get(index) method in Java with examples
- ArrayList listIterator() method in Java with Examples
- ArrayList size() method in Java with Examples
- ArrayList subList() method in Java with Examples
- ArrayList clear() in Java with examples
- Arraylist removeRange() in Java with examples
- ArrayList and LinkedList remove() methods in Java with Examples
- ArrayList spliterator() method in Java
- ArrayList retainAll() method in Java
- ArrayList removeIf() method in Java
- ArrayList forEach() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



