-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathVectorAccessElementDemo.java
More file actions
37 lines (29 loc) · 963 Bytes
/
VectorAccessElementDemo.java
File metadata and controls
37 lines (29 loc) · 963 Bytes
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
33
34
35
36
37
/**
* The VectorAccessElementDemo program implements an application that
* simply demonstrates the different ways to Access elements in Vector
*
* @author Sarju S
* @version 1.0
* @since 2020-09-24
*/
package com.sjcet.basicPrograms;
import java.util.Iterator;
import java.util.Vector;
public class VectorAccessElementDemo {
public static void main(String[] args) {
Vector<String> animals= new Vector<>();
animals.add("Dog");
animals.add("Horse");
animals.add("Cat");
// Using get()
String element = animals.get(2);
System.out.println("Element at index 2: " + element);
// Using iterator()
Iterator<String> iterate = animals.iterator();
System.out.print("Vector: ");
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}//end of while
}//end of main
}//end of class