Vector ensureCapacity() method in Java with Example Last Updated : 24 Dec, 2018 Comments Improve Suggest changes Like Article Like Report The ensureCapacity() method of Java.util.Vector class increases the capacity of this Vector 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 // 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 Vector<Integer> Vector<Integer> vector = new Vector<Integer>(); // adding element to vector vector.add(10); vector.add(20); vector.add(30); vector.add(40); // Print the Vector System.out.println("Vector: " + vector); // ensure that the Vector // can hold upto 5000 elements // using ensureCapacity() method vector.ensureCapacity(5000); // Print System.out.println("Vector can now" + " surely store upto" + " 5000 elements."); } catch (Exception e) { System.out.println(e); } } } Output: Vector: [10, 20, 30, 40] Vector can now surely store upto 5000 elements. Example 2: Java // 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 Vector<Integer> Vector<String> vector = new Vector<String>(); // adding element to vector vector.add("A"); vector.add("B"); vector.add("C"); vector.add("D"); // Print the Vector System.out.println("Vector: " + vector); // ensure that the Vector // can hold upto 400 elements // using ensureCapacity() method vector.ensureCapacity(400); // Print System.out.println("Vector can now" + " surely store upto" + " 400 elements."); } catch (Exception e) { System.out.println(e); } } } Output: Vector: [A, B, C, D] Vector can now surely store upto 400 elements. Comment More infoAdvertise with us Next Article Vector ensureCapacity() method in Java with Example S SandySharma Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2018 Practice Tags : Java Similar Reads Vector set() Method in Java The Java.util.Vector.set() method is used to replace any particular element in the vector, created using the Vector class, with another element. Syntax: Vector.set(int index, Object element) Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below. 2 min read Vector remove() Method in Java remove(int index) The java.util.vector.remove(int index) method is used to remove an element from a Vector from a specific position or index. Syntax: Vector.remove(int index) Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element 2 min read Vector get() Method in Java The java.util.vector.get() method is used to fetch or retrieve an element at a specific index from a Vector. Syntax: Vector.get(int index) Parameters: This method accepts a mandatory parameter index which is of integer data type. It specifies the position or index of the element to be fetched from t 2 min read Vector addAll() Method in Java In Java, the addAll() method is used to add all elements from a specified Collection to the current Vector. The addAll() method allows you to efficiently append multiple elements to a vector in one operation.Implementation:Java// Java Program to addAll elements // From Vector to Vector import java.u 3 min read Vector addElement() Method in Java The Java.util.Vector.addElement() method is used to append a specified element to the end of this vector by increasing the size of the vector by 1. The functionality of this method is similar to that of the add() method of Vector class.Syntax: boolean addElement(Object element)Parameters: This funct 2 min read Vector capacity() Method in Java The Java.util.Vector.capacity() method in Java is used to get the capacity of the Vector or the length of the array present in the Vector.Syntax of Vector capacity() methodVector.capacity()Parameters: The method does not take any parameter. Return Value: The method returns the capacity or the intern 2 min read Vector clear() Method in Java The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax:Vector.clear( 2 min read Java Vector clone() Method In Java, the clone() method of the Vector class is used to return a shallow copy of the vector. It just creates a copy of the vector. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array.Example 1: In this example, we use the cl 3 min read Java Vector contains() Method The contains() method in Java is used to check whether a specific element is present in the Vector or not.Example 1: In this example, we will check whether a particular string is present in the vector or not.Java// Java program to demonstrate the use // of the contains() method with Strings import j 3 min read Java Vector containsAll() Method In Java, the containsAll() method is used to check if the Vector contains all the elements of a specified Collection or not.Example 1: Below program demonstrating the use of containsAll() method with Strings.Java// Java Program to demonstrate the use of // containsAll() with Strings import java.util 2 min read Like