- Trending Categories
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
Clear out all of the Vector elements in Java
A Vector can be cleared in Java using the method java.util.Vector.clear(). This method removes all the elements in the Vector. There are no parameters required by the Vector.clear() method and it returns no value.
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); vec.add(2); vec.add(8); System.out.println("The Vector elements are: " + vec); vec.clear(); System.out.println("The Vector elements are: " + vec); } }
The output of the above program is as follows −
The Vector elements are: [4, 1, 3, 9, 6, 2, 8] The Vector elements are: []
Now let us understand the above program.
The Vector is created. Then Vector.add() is used to add the elements to the Vector. The Vector is displayed before and after using the Vector.clear() method which clears the Vector. 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); vec.add(2); vec.add(8); System.out.println("The Vector elements are: " + vec); vec.clear(); System.out.println("The Vector elements are: " + vec);
- Related Articles
- Replace all the elements of a Vector with Collections.fill() in Java
- Append all elements of another Collection to a Vector in Java
- Enumerate through the Vector elements in Java
- Add elements at the middle of a Vector in Java
- Add elements at the end of a Vector in Java
- Add elements to a Vector in Java
- Removing Single Elements in a Vector in Java
- Java Program to Shuffle Vector Elements
- Loop through the Vector elements using an Iterator in Java
- Loop through the Vector elements using a ListIterator in Java
- How to copy elements of ArrayList to Java Vector
- How to find all combinations of a vector elements without space in R?
- How can we clear all selections in Java Swing JList?
- Get first and last elements from Vector in Java
- The clear() method of AbstractSequentialList in Java

Advertisements