Java Vector toString() Example

The toString() method of java.util.Vector class returns a string representation of the Vector, containing the String representation of each element.

Signature

public String toString()

This method returns a string representation of the Vector, containing the String representation of each element.

Example

The following example shows the usage of java.util.Vector.toString() method.

import java.util.Vector;

public class ToStringMethodVectorExample 
{
    public static void main(String[] args) { 
        // Create Vector vector1 object
        Vector vector1 = new Vector();
        
        //adding elements to vector1
        vector1.add("Element1");
        vector1.add("Element2");
        vector1.add("Element3");
        vector1.add("Element4");
        vector1.add("Element5");
        vector1.add("Element6");
        vector1.add("Element7");
        
        String element = vector1.toString();
        
        System.out.println("**Contents of vector1**");
        System.out.println(element);
    }
}

Output

**Contents of vector1**
[Element1, Element2, Element3, Element4, Element5, Element6, Element7]

Leave a Reply

Your email address will not be published. Required fields are marked *