- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print a Vector in a comma-delimited list, in index order and surrounded by square brackets ([]) in Java
A Vector can be printed in a comma-delimited list, in index order and surrounded by square brackets ([]) by simply using System.out.println() along with the Vector object.
A program that demonstrates this is given as follows −
Example
import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(5); vec.add(4); vec.add(1); vec.add(3); vec.add(9); vec.add(6); System.out.println(vec); } }
The output of the above program is as follows −
[4, 1, 3, 9, 6]
Now let us understand the above program.
The Vector is created. Then Vector.add() is used to add the elements to the Vector. Finally, the Vector elements are printed in a comma-delimited list, in index order and surrounded by square brackets ([]). A code snippet which demonstrates this is as follows −
Vector vec = new Vector(5); vec.add(4); vec.add(1); vec.add(3); vec.add(9); vec.add(6); System.out.println(vec);
Advertisements