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.

Syntax

Following is the declaration of setSize() method:

Parameter

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

Return

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

Exceptions

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

Output:

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

Output:

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

Output:

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA