YouTip LogoYouTip

Java Vector Elements

Java Vector elements() Method \\n \\n\\n \\n\\n
\\n

Java Vector elements() Method

\\n\\n

Java Tutorial

\\n \\n\\n

Java Object-Oriented Programming

\\n \\n\\n

Java Advanced Tutorial

\\n \\n\\n

Java Vector elements() Method

\\n

The `elements()` method is an important method provided by the `Vector` class in Java. It returns an `Enumeration` object that can be used to iterate over all the elements in the `Vector`. This method belongs to the Java Collections Framework and is mainly used for compatibility maintenance with old code from the Java 1.0 era.

\\n\\n

Method Declaration

\\n
public Enumeration<E> elements()
\\n\\n

Returns

\\n

Returns an enumeration of the elements in this vector (an `Enumeration` object).

\\n\\n

Type Parameters

\\n
\\n
E
\\n
The type of elements in the vector.
\\n
\\n\\n

Usage Example

\\n

Example

\\n
import java.util.Vector;\\nimport java.util.Enumeration;\\n\\npublic class VectorElementsExample {\\n    public static void main(String[] args) {\\n        // Create a Vector and add elements\\n        Vector<String> fruits = new Vector();\\n        fruits.add("Apple");\\n        fruits.add("Banana");\\n        fruits.add("Orange");\\n\\n        // Get Enumeration using elements() method\\n        Enumeration<String> enumeration = fruits.elements();\\n\\n        // Iterate over the elements in the Vector\\n        System.out.println("Vector Elements:");\\n        while (enumeration.hasMoreElements()) {\\n            System.out.println(enumeration.nextElement());\\n        }\\n    }\\n}
\\n
← Java Vector EqualsJava Vector Copyinto β†’