VOOZH about

URL: https://www.geeksforgeeks.org/java/vector-setsize-method-in-java-with-example/

⇱ Vector setSize() method in Java with Example - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Vector setSize() method in Java with Example

Last Updated : 11 Jul, 2025

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:


Output
Initially
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 


Output
Initially
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 

Output:

👁 Image

Comment