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:
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.
Java 1.2 and above
Example 1Test 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 2Test it Now
Components of a vector: [White, Green, Black] Components of a vector after setting new size: [White, Green, Black, null, null, null]
Example 3Test it Now
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)