Java Utility Library

Java Vector - toString() Method



The java.util.Vector.toString() method returns a string representation of the vector which contains each element represented in string.

Syntax

public String toString()

Parameters

No parameter is required.

Return Value

Returns string representation of the vector.

Exception

NA.

Example:

In the example below, the java.util.Vector.toString() method returns a string representation of the given vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);   

    //convert the vector into a string
    String MyStr = MyVector.toString();

    //print the string
    System.out.println(MyStr);
  }
}

The output of the above code will be:

[10, 20, 30]

❮ Java.util - Vector