Java Vector size() setSize() Methods Tutorial

In this section, we will learn what the Vector size(), and setSize() methods are and how to use them in Java.

What is Java Vector size() Method?

The Java Vector size() method is used to get the current number of elements in a Vector object.

Note: this method only returns the current number of elements, but not the capacity of a vector object. This means if there’s no element in a vector object but its capacity is set to any value higher than 0, we still get the value 0 instead.

Java Vector size() Method Syntax:

public int size()

size() Method Parameters:

The method does not take an argument.

size() Method Return Value:

The return value of the method is of type integer and is equal to the current number of elements in the target vector object.

size() Method Exceptions:

The method does not throw an exception.

Example: using Vector size() method

import java.util.Vector; 
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.add("Jack");
        vector.add("Omid");
        vector.add("Ellen");
        vector.add("John");

        System.out.println(vector.size());
    }
}

Output:

4

What is Java Vector setSize() Method?

The Java Vector setSize() method is used to change the size (the number of elements) of a vector object.

By default, this size is equal to the current number of elements in a vector object. But we can change this size using the setSize() method.

Note:

  • If the value we set for this method is higher than the current size of the target vector object, the new elements will have the value null as their default values.
  • If the value we set for this method is less than the current size of the target vector object, enough elements from the end of the target vector object will be removed in order to match the size to the value we’ve passed to this method.

Java Vector setSize() Method Syntax:

public void setSize(int newSize)

setSize() Method Parameters:

The method takes one argument and that is the new size we want to set for the target vector object.

setSize() Method Return Value:

The return value of this method is void.

setSize() Method Exceptions:

The method might throw one exception and that is:

ArrayIndexOutOfBoundsException: We get this exception if the argument of the method is a negative value.

Example: using Vector setSize() method

import java.util.Vector; 
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.add("Jack");
        vector.add("Omid");
        vector.add("Ellen");
        vector.add("John");

        vector.setSize(8);

        for (String s: vector){
         System.out.println(s);
        }
    }
}

Output:

Jack

Omid

Ellen

John

null

null

null

null

As you can see, the new elements of this vector have the null as their default values.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies