-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWithListIterator2.java
More file actions
57 lines (49 loc) · 1.5 KB
/
Copy pathWithListIterator2.java
File metadata and controls
57 lines (49 loc) · 1.5 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ListIterator;
import java.util.Iterator;
public class WithListIterator2 {
public static void main(String[] args) {
List<Student> studentList1 = new ArrayList<Student>();
Student s1 = new Student("Sujit",1);
Student s2 = new Student("Siddharth",2);
Student s3 = new Student("Karanpreet",3);
Student s4 = new Student("Hari",5);
Student s5 = new Student("Tricha",4);
studentList1.add(s1);
studentList1.add(s2);
studentList1.add(s3);
studentList1.add(s4);
studentList1.add(s5);
//iterateFwd(studentList1);
iterateBkwd(studentList1);
}
private static void iterateFwd(List<Student> students) {
ListIterator<Student> it = students.listIterator();
System.out.println("Printing student list...");
while(it.hasNext()) {
System.out.println(it.next().getDetails());
}
}
private static void iterateBkwd(List<Student> students) {
ListIterator<Student> it = students.listIterator(students.size());
System.out.println("Printing student list...");
while(it.hasPrevious()) {
System.out.println(it.previous().getDetails());
}
}
}
class Student {
private final String name;
private final int rollNumber;
public Student(String name, int rollNumber) {
this.name = name;
this.rollNumber = rollNumber;
}
public String getDetails() {
return
"name = " + this.name + '\n' +
"roll number = " + this.rollNumber + '\n';
}
}