Vector setSize() method in Java with Example
The Java.util.Vector.setSize() is a method of Vector class which is used to set the new size of vector. If new size of vector is greater than the current size then null elements are added to the vector is new size is less than current size then all higher order elements are deleted. If new size size is negative then there will be Runtime Error : ArrayIndexOutOfBoundsException
public void setSize(int newSize)
Parameters:This method accepts a mandatory parameter newSize of vector.
Return Type: This method modified the size of the Vector to the newSize and does not return anything.
Exceptions: This method throws ArrayIndexOutOfBoundsException if new size is negative.
Below examples illustrates the working of Vector.setSize() method:
Initially Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad] Size: 6 After using setSize() Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad, null, null] Size: 8
Initially Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad] Size: 6 After using setSize() Vector: [Geeks, for, Geeks, Ankit] Size: 4
When new size is negative
Initially Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad] Size: 6 Trying to change size to '-8' java.lang.ArrayIndexOutOfBoundsException: -8
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.