Skip to content
Related Articles

Related Articles

Improve Article

Vector setSize() method in Java with Example

  • Last Updated : 24 Dec, 2018

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());
    }
}
Output:
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());
    }
}
Output:
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);
        }
    }
}
Output:
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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :