Question
Question about Iterator implementation in Java I figured out how implement next() and hasNext() method, but I am confused about how implement previous() and hasPrevious()
Question about Iterator implementation in Java
I figured out how implement next() and hasNext() method, but I am confused about how implementprevious()andhasPrevious()method.,
Please help me with this practice exercise.
Main.java
import java.util.*;
public class Main {
public static void main(String[] args) {
ListOfStudents ls = new ListOfStudents();
StudentsIterator iterator = ls.getStudents();
LinkedList list = new LinkedList();
ListIterator it2 = list.listIterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
System.out.println(iterator.hasNext());
}
it2.next();
it2.next();
it2.next();
while (it2.hasPrevious()) {
System.out.println(it2.previous());
}
}
}
class ListOfStudents {
private String[] students;
public ListOfStudents() {
students = new String[6];
students[0] = "Amy";
students[1] = "Rosa";
students[2] = "Jason";
students[3] = "Caitlyn";
students[4] = "Simon";
students[5] = "Andy";
}
public StudentsIterator getStudents() {
return new StudentsIterator() {
int i = 0;
public String next() {
String currentStudent = students[i];
i++;
return currentStudent;
}
public String previous() {
return
}
public boolean hasNext() {
return i
}
public boolean hasPrevious() {
return
}
};
}
}
interface StudentsIterator {
String next();
String previous();
boolean hasNext();
boolean hasPrevious();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started