The java.util.Vector.toString() is an inbuilt method of Vector that is used to get a string representation of the objects of Vector in the form of a String representation of entries separated by “, “. So basically the toString() method is used to convert all the elements of Vector into String.
Syntax:
Vector.toString()
Parameter: The method does not take any parameters.
Return value: The method returns the String representation consisting of the string representation of the elements of the Vector.
Below programs illustrate the working of java.util.Vector.toString() method:
Program 1:
// Java code to illustrate the toString() method import java.util.*; public class GFG { public static void main(String[] args) { // creating vector type object Vector<String> vector = new Vector<String>(); // Inserting elements into the table vector.add("Geeks"); vector.add("4"); vector.add("Geeks"); vector.add("Welcomes"); vector.add("You"); // Displaying the Vector System.out.println("Vector: " + vector); // Displaying the string representaion System.out.println("The String representation is: " + vector.toString()); } } |
Vector: [Geeks, 4, Geeks, Welcomes, You] The String representation is: [Geeks, 4, Geeks, Welcomes, You]
Program 2:
// Java code to illustrate the toString() method import java.util.*; public class GFG { public static void main(String[] args) { // Creating an empty Vector Vector<Integer> vector = new Vector<Integer>(); // Inserting elements into the table vector.add(10); vector.add(15); vector.add(20); vector.add(25); vector.add(30); // Displaying the Vector System.out.println("Initial Vector is: " + vector); // Displaying the string representaion System.out.println("The String representation is: " + vector.toString()); } } |
Initial Vector is: [10, 15, 20, 25, 30] The String representation is: [10, 15, 20, 25, 30]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- LinkedHashSet toString() method in Java with Example
- HashSet toString() method in Java with Example
- Stack toString() method in Java with Example
- AbstractSet toString() method in Java with Example
- AbstractSequentialList toString() method in Java with Example
- TreeSet toString() method in Java with Example
- DecimalStyle toString() method in Java with Example
- FieldPosition toString() method in Java with Example
- ParsePosition toString() method in Java with Example
- HijrahDate toString() method in Java with Example
- JapaneseDate toString() method in Java with Example
- MinguoDate toString() method in Java with Example
- ThaiBuddhistDate toString() method in Java with Example
- ZoneOffsetTransition toString() method in Java with Example
- vector :: cbegin() and vector :: cend() in C++ STL
- Vector removeElement() method in Java with Example
- Vector removeRange() method in Java with Example
- Vector setSize() method in Java with Example
- Vector trimToSize() method in Java with Example
- Vector setElementAt() method in Java with Example
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.

