-
Notifications
You must be signed in to change notification settings - Fork 2
/
ListIterator.java
46 lines (37 loc) · 1.28 KB
/
ListIterator.java
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
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;
import java.util.ListIterator;
public class ListIteratorPractice {
public static void main(String[] args) {
// Creating an object of ArrayList class
ArrayList al = new ArrayList();
// Iterating over Arraylist object
for (int i = 0; i < 10; i++)
// Adding elements to the Arraylist object
al.add(i);
// Print and display all elements inside object
// created above
System.out.println(al);
// At beginning ltr(cursor) will point to
// index just before the first element in al
ListIterator ltr = al.listIterator();
// Checking the next element availability
while (ltr.hasNext()) {
// Moving cursor to next element
int i = (Integer)ltr.next();
// Getting even elements one by one
System.out.print(i + " ");
// Changing even numbers to odd and
// adding modified number again in
// iterator
if(i%2==0) {
// Set method to change value
ltr.set(99);
// To add
ltr.add(100);
}
}
// Print and display statements
System.out.println();
System.out.println(al);
}
}