Java Vector elements() Example

The elementAt(int index) method of java.util.Vector class will get you the element at the specified index whereas elements() method will gets you the enumeration of the elements of the vector. 

Signature

public Enumeration<E> elements() 

Returns an enumeration of the elements of the vector. The returned Enumeration object will contain all the elements of the vector.

Example

The following example shows the usage of java.util.Vector.element() method.

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

public class ElementsMethodVectorExample 
{
	public static void main(String args[])
	{
		// Create vector1 object
		Vector vector1 = new Vector();
		
		//adding elements to Vector using add()
		vector1.add("Element1");
		vector1.add("Element2");
		vector1.add("Element3");
		vector1.add("Element4");
		
	    //Get enumeration of elements
		Enumeration enumeration = vector1.elements(); 
		
		//Printing the elements of the Vector1
		System.out.println("Elements of vector1");
		while(enumeration.hasMoreElements())
		{
		    System.out.println(enumeration.nextElement());
		}
	}
}

Output

Elements of vector1
Element1
Element2
Element3
Element4

Leave a Reply

Your email address will not be published. Required fields are marked *