Home » Java programming language

Java Vector removeAllElements() Method with Example

Vector Class removeAllElements() method: Here, we are going to learn about the removeAllElements() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 19, 2020

Vector Class removeAllElements() method

  • removeAllElements() method is available in java.util package.
  • removeAllElements() method is used to remove all the existing elements from this Vector.
  • removeAllElements() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • removeAllElements() method does not throw an exception at the removing elements.

Syntax:

    public void removeAllElements();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void removeAllElements() method of Vector 

import java.util.*;

public class RemoveAllElementsOfVector {
 public static void main(String[] args) {
  // Instantiates a Vector object  with
  // initial capacity of "10"
  Vector < String > v = new Vector < String > (10);

  // By using add() method is to add the
  // elements in this v
  v.add("C");
  v.add("C++");
  v.add("JAVA");

  // Display Vector 
  System.out.println("v: " + v);

  // By using removeAllElements() method
  // is to remove all the existing elements
  v.removeAllElements();

  // Display updated vector
  System.out.println("v.removeAllElements(): " + v);
 }
}

Output

v: [C, C++, JAVA]
v.removeAllElements(): []


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.