-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorIteratorDemo.java
More file actions
32 lines (25 loc) · 1.07 KB
/
Copy pathVectorIteratorDemo.java
File metadata and controls
32 lines (25 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Vector;
import java.util.Iterator;
public class VectorIteratorDemo {
public static void main(String[] args) {
System.out.println("--- Vector and Iterator Demonstration ---");
// 1. Create a Vector of String objects
Vector<String> namesVector = new Vector<>();
// 2. Add five names to the Vector
namesVector.add("Alice");
namesVector.add("Bob");
namesVector.add("Charlie");
namesVector.add("Diana");
namesVector.add("Evan");
System.out.println("Successfully added 5 names to the Vector.\n");
System.out.println("--- Traversing Vector using Iterator ---");
// 3. Obtain an Iterator from the Vector
Iterator<String> iterator = namesVector.iterator();
// 4. Print each name using the Iterator's hasNext() and next() methods
while (iterator.hasNext()) {
String currentName = iterator.next();
System.out.println("Name: " + currentName);
}
System.out.println("----------------------------------------");
}
}