Home » Java programming language

Java Vector setSize() Method with Example

Vector Class setSize() method: Here, we are going to learn about the setSize() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 19, 2020

Vector Class setSize() method

  • setSize() method is available in java.util package.
  • setSize() method is used to set the new size of this vector and when new size (n_size) > current size then extended size will be set to null initially.
  • setSize() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • setSize() method may throw an exception at the time of setting the size of this Vector.
    ArrayIndexOutOfBoundsException: This exception may throw when the given parameter is not in a range.

Syntax:

    public void setSize(int n_size);

Parameter(s):

  • int n_size – represents the new size to be set for this Vector.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void setSize(int n_size) method 
// of Vector 

import java.util.*;

public class SetSizeOfVector {
 public static void main(String[] args) {
  // Instantiates a Vector object  with
  // initial capacity of "10"
  Vector < String > v = new Vector < String > (10);

  // By using add() method is to add the
  // elements in this v
  v.add("C");
  v.add("C++");
  v.add("JAVA");

  // Display Vector
  System.out.println("v: " + v.size());

  // By using setSize() method is to
  // set the new size of this vector
  v.setSize(20);

  // Display updated size
  System.out.println("v.setSize(20): " + v.size());
 }
}

Output

v: 3
v.setSize(20): 20



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.