Java Vector.elements() Method

 Posted On  | Yashwant Chavan 

In this tutorial, you will learn about Java Vector.elements() Method. This method returns an enumeration of the elements of this Vector.

syntax

Returns an enumeration of the elements of this vector. The returned Enumeration object will generate all items in this vector. The first item generated is the item at index 0, then the item at index 1, and so on.

Enumeration<E> java.util.Vector.elements()

Return

returns an enumeration of the elements of this vector

Java example to get Vector elements

  • Create Vector instance using new Vector().
  • Add some elements in the Vector.
  • elements() method is used to get the enumeration of the Vector elements. Enumerate each Vector element and print the value.
package com.technicalkeeda.app;

import java.util.Enumeration;
import java.util.Vector;

public class VectorElements {
    public static void main(String[] args) {

        Vector < String > colors = new Vector < String > ();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        colors.add("Pink");

        Enumeration < String > enumeration = colors.elements();
        while (enumeration.hasMoreElements()) {
            System.out.println("Color:- " + enumeration.nextElement());
        }

    }
}

Output

Color:- Red
Color:- Green
Color:- Blue
Color:- Pink


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0303 seconds.