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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!