iterator() method of Vector class that is present inside java.util package is used to return an iterator of the same elements as that of the Vector. The elements are returned in random order from what was present in the vector.
Syntax:
Iterator iterate_value = Vector.iterator();
Parameters: The function does not take any parameter.
Return Type: The method iterates over the elements of the vector and returns the values(iterators).
Example 1:
Java
import java.util.*;
import java.util.Vector;
public class GFG {
public static void main(String args[])
{
Vector<String> vector = new Vector<String>();
vector.add("Welcome");
vector.add("To");
vector.add("Geeks");
vector.add("4");
vector.add("Geeks");
System.out.println("Vector: " + vector);
Iterator value = vector.iterator();
System.out.println("The iterator values are: ");
while (value.hasNext()) {
System.out.println(value.next());
}
}
}
|
Output: Vector: [Welcome, To, Geeks, 4, Geeks]
The iterator values are:
Welcome
To
Geeks
4
Geeks
Example 2:
Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
Vector<Integer> vector = new Vector<Integer>();
vector.add(10);
vector.add(20);
vector.add(30);
vector.add(40);
vector.add(50);
System.out.println("Vector: " + vector);
Iterator value = vector.iterator();
System.out.println("The iterator values are: ");
while (value.hasNext()) {
System.out.println(value.next());
}
}
}
|
Output: Vector: [10, 20, 30, 40, 50]
The iterator values are:
10
20
30
40
50
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
17 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...