8. void ensureCapacity(int minCapacity)

8. void ensureCapacity(int minCapacity)
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.spy.arraylist.method;
 
import java.util.ArrayList;
 
public class IncreaseSize 
{
 
	public static void main(String[] args)
	{
 
        ArrayList<String> emp_list=new ArrayList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
		//Increasing size of ArrayList
		emp_list.ensureCapacity(15);
 
	    //Retrieving Element
		for (String s : emp_list) 
		{
			System.out.println(s);
		}
 
	}
}

Output:

1
2
3
4
Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Vaibhav Prajapati

Leave a Reply

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