ArrayList.ensureCapacity(int): This method is available in java.util.ArrayList class of Java.
Syntax:
void java.util.ArrayList.ensureCapacity(int minCapacity)
This method takes one argument of type int as its parameter. This method 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.
Parameters: One parameter is required for this method.
minCapacity: the desired minimum capacity.
Returns: NA
Exceptions: NA
Approach
Java
import java.util.ArrayList;public class ArrayListensureCapacity {public static void main(String[] args) {ArrayList<Integer> arr = new ArrayList<>();arr.ensureCapacity(4);arr.add(1);arr.add(2);arr.add(3);System.out.println(arr.size());}}
Output:
3
No comments:
Post a Comment