Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Management Accounting Information for Decision-Making and Strategy Execution

Authors: Anthony A. Atkinson, Robert S. Kaplan, Ella Mae Matsumura, S. Mark Young

6th Edition

137024975, 978-0137024971

Students also viewed these Programming questions

Question

please try to give correct answer 1 8 3 .

Answered: 1 week ago