Home » Java programming language

Java Vector elements() Method with Example

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

Vector Class elements() method

  • elements() method is available in java.util package.
  • elements() method is used to get an enumeration of the elements that exist in this Vector.
  • elements() 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.
  • elements() method does not throw an exception at the time of returning elements in an Enumeration view.

Syntax:

    public Enumeration elements();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is Enumeration, it returns an Enumeration that contain all objects of this Vector.

Example:

// Java program to demonstrate the example 
// of Enumeration elements() method 
// of Vector 

import java.util.*;

public class ElementsOfVector {
    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 elements() method is to
        // get all the elements into an Enumeration
        // and display it with the help of for loop
        System.out.println("Enumeration: ");
        for (Enumeration en = v.elements(); en.hasMoreElements();)
            System.out.println(en.nextElement());
    }
}

Output

v: [C, C++, JAVA]
Enumeration: 
C
C++
JAVA



Comments and Discussions!

Load comments ↻






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