Vector setSize() method in Java with Example Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The Java.util.Vector.setSize() is a method of Vector class that is used to set the new size of the vector. If the new size of the vector is greater than the current size then null elements are added to the vector is new size is less than the current size then all higher-order elements are deleted. This method modified the size of the Vector to the newSize and does not return anything. Syntax: public void setSize(int newSize) Parameters: This method accepts a mandatory parameter newSize of the vector. Return Type: NA Exceptions: ArrayIndexOutOfBoundsException Note: If new size is negative then it will throw Runtime Error Example 1: Java // Java Program to Illustrate setSize() method // of Vector class // Importing required classes import java.util.*; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating object of Vector class // Declaring object of string type Vector<String> v = new Vector<String>(); // Inserting elements into the vector // using add() method // Custom input elements v.add("Geeks"); v.add("for"); v.add("Geeks"); v.add("Computer"); v.add("Science"); v.add("Portal"); // Printing vector before calling setSize() method System.out.println("Initially"); System.out.println("Vector: " + v); System.out.println("Size: " + v.size()); // Setting new custom size v.setSize(8); // Printing vector after calling setSize() System.out.println("\nAfter using setSize()"); System.out.println("Vector: " + v); System.out.println("Size: " + v.size()); } } OutputInitially Vector: [Geeks, for, Geeks, Computer, Science, Portal] Size: 6 After using setSize() Vector: [Geeks, for, Geeks, Computer, Science, Portal, null, null] Size: 8 Example 2: When new size is positive Java // Java program to Illustrate setSize() method // of Vector class // Where size is positive // Importing utility classes import java.util.*; // Main class public class GFG { // amin driver method public static void main(String[] args) { // Creating vector object of string type Vector<String> v = new Vector<String>(); // Inserting elements into the vector //. using add() method // Custom input elements v.add("Geeks"); v.add("for"); v.add("Geeks"); v.add("Computer"); v.add("Science"); v.add("Portal"); // Printing vector before calling setSize() System.out.println("Initially"); System.out.println("Vector: " + v); System.out.println("Size: " + v.size()); // Setting new size v.setSize(4); // Printing vector after calling setSize() System.out.println("\nAfter using setSize()"); System.out.println("Vector: " + v); System.out.println("Size: " + v.size()); } } OutputInitially Vector: [Geeks, for, Geeks, Computer, Science, Portal] Size: 6 After using setSize() Vector: [Geeks, for, Geeks, Computer] Size: 4 Example 3: When the new size is negative Java // Java program to Illustrate setSize() method // of vector class // Where size is negative // Importing utility classes import java.util.*; // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating Vector class object of string type Vector<String> v = new Vector<String>(); // Inserting elements into the vector // using add() method v.add("Geeks"); v.add("for"); v.add("Geeks"); v.add("Computer"); v.add("Science"); v.add("Portal"); // Printing vector before calling setSize() System.out.println("Initially"); System.out.println("Vector: " + v); System.out.println("Size: " + v.size()); // Try block to check for exceptions try { // Setting new size v.setSize(-8); } // Catch block to handle exceptions catch (Exception e) { // Display message when exceptions occurred System.out.println("Trying to change " + "size to '-8'\n" + e); } } } Output: Comment More infoAdvertise with us Next Article Vector listIterator() method in Java with Examples A ankit15697 Follow Improve Article Tags : Java Technical Scripter Java-Collections Java-Functions Java-Vector +1 More Practice Tags : JavaJava-Collections Similar Reads Vector isEmpty() Method in Java The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris 2 min read Vector iterator() method in Java with Examples iterator() method of Vector class that is present inside java.util package is used to return an iterator of the same elements as that of the Vector. The elements are returned in random order from what was present in the vector. Syntax: Iterator iterate_value = Vector.iterator(); Parameters: The func 2 min read Vector lastElement() Method in Java The java.util.vector.lastElement() method in Java is used to retrieve or fetch the last element of the Vector. It returns the element present at the last index of the vector. Syntax: Vector.lastElement() Parameters: The method does not take any parameter. Return Value: The method returns the last el 2 min read Vector lastIndexOf() Method in Java The Java.util.Vector.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present in the vector then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is us 2 min read Vector listIterator() method in Java with Examples java.util.Vector.listIterator() This method returns a list iterator over the elements of a Vector object in proper sequence. It is bidirectional, so both forward and backward traversal is possible, using next() and previous() respectively. The iterator thus returned is fail-fast. This means that str 3 min read Vector removeAll() Method in Java The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified. Syntax: Vector.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed f 2 min read Vector removeAllElements() method in Java with Example The Java.util.Vector.removeAllElements() method is used to removes all components from this Vector and sets its size to zero. Syntax: Vector.removeAllElements() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Jav 2 min read Vector removeElement() method in Java with Example The java.util.Vector.removeElement() method is used to remove first occurrence of particular object. If object is not found then it returns false else it returns true. If a particular object is present inside vector and removeElement() method call on that vector element then this method reduces vect 3 min read Vector removeElementAt() Method in Java The java.util.vector.removeElementAt(int index) method is used to remove an element from a Vector from a specific position or index. In this process the size of the vector is automatically reduced by one and all other elements after the removed element are shifted downwards by one position. Syntax: 2 min read Vector removeIf() method in Java The removeIf() method of Vector removes all of those elements from Vector which satisfies the condition passed as a parameter to this method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or 3 min read Like