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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Initially
Vector: [Geeks, for, Geeks, Ankit, MNNIT, Allahabad]
Size: 6
Trying to change size to '-8'
java.lang.ArrayIndexOutOfBoundsException: -8


My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

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.