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
Syntax:
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:
Example 1:
// Java program to understand // about vector.setSize() method import java.util.*; public class GFG { public static void main(String[] args) { // creating vector type object Vector<String> v = new Vector<String>(); // inserting elements into the vector v.add( "Geeks" ); v.add( "for" ); v.add( "Geeks" ); v.add( "Ankit" ); v.add( "MNNIT" ); v.add( "Allahabad" ); // printing vector before calling setSize() System.out.println( "Initially" ); System.out.println( "Vector: " + v); System.out.println( "Size: " + v.size()); // setting new size v.setSize( 8 ); // printing vector after calling setSize() System.out.println( "\nAfter using setSize()" ); System.out.println( "Vector: " + v); System.out.println( "Size: " + v.size()); } } |
Initially Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad] Size: 6 After using setSize() Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad, null, null] Size: 8
Example 2:
// Java program to understand // about vector.setSize() method import java.util.*; public class GFG { public static void main(String[] args) { // creating vector type object Vector<String> v = new Vector<String>(); // inserting elements into the vector v.add( "Geeks" ); v.add( "for" ); v.add( "Geeks" ); v.add( "Ankit" ); v.add( "MNNIT" ); v.add( "Allahabad" ); // printing vector before calling setSize() System.out.println( "Initially" ); System.out.println( "Vector: " + v); System.out.println( "Size: " + v.size()); // setting new size v.setSize( 4 ); // printing vector after calling setSize() System.out.println( "\nAfter using setSize()" ); System.out.println( "Vector: " + v); System.out.println( "Size: " + v.size()); } } |
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
// Java program to understand // about vector.setSize() method // because vector is present in this package import java.util.*; // Driver Code public class GFG { // main method begins here public static void main(String[] args) { // creating vector type object Vector<String> v = new Vector<String>(); // inserting elements into the vector v.add( "Geeks" ); v.add( "for" ); v.add( "Geeks" ); v.add( "Ankit" ); v.add( "MNNIT" ); v.add( "Allahabad" ); // printing vector before calling setSize() System.out.println( "Initially" ); System.out.println( "Vector: " + v); System.out.println( "Size: " + v.size()); try { // setting new size v.setSize(- 8 ); } catch (Exception e) { System.out.println( "Trying to change " + "size to '-8'\n" + e); } } } |
Initially Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad] Size: 6 Trying to change size to '-8' java.lang.ArrayIndexOutOfBoundsException: -8
Recommended Posts:
- Stack setSize() method in Java with Example
- Vector contains() Method in Java
- Vector set() Method in Java
- Vector get() Method in Java
- Vector add() Method in Java
- Vector subList() Method in Java
- Vector addElement() Method in Java
- Vector elements() Method in Java
- Vector containsAll() Method in Java
- Vector size() Method in Java
- Vector ensureCapacity() method in Java with Example
- Vector hashCode() Method in Java
- Vector insertElementAt() Method in Java
- Vector copyInto() Method in Java
- Vector equals() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.