Javatpoint Logo
Javatpoint Logo

Java Vector setSize() Method

The setSize() method of Java Vector class is used to set the size of a vector. If the new size is greater than the current size, null items are added to the end of the vector. Otherwise, all components at index newSize and greater are discarded.


Following is the declaration of setSize() method:


Parameter Description Required/Optional
newSize It is the new size of the vector. Required


The setSize() method does not return anything. It only set the new size of the vector.


ArrayIndexOutOfBoundsException- This method has thrown an exception if the new size of the vector is negative i.e. newSize < 0.

Compatibility Version

Java 1.2 and above

Example 1

Test it Now


Components of the vector: [1, 2, 3]
Components of the vector after setting new size: 
Number = 1
Number = 2
Number = 3
Number = null
Number = null
Number = null

Example 2

Test it Now


Components of a vector: [White, Green, Black]
Components of a vector after setting new size: [White, Green, Black, null, null, null]

Example 3

Test it Now


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -15
	at java.base/java.util.Vector.setSize(
	at myPackage.VectorSetSizeExample3.main(

Youtube For Videos Join Our Youtube Channel: Join Now


  • Send your Feedback to

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA