java - Returning values from a linked list without moving the iterator -
i have 2 classes: student , registry.registry designed hold list of students. when try return valuse of students registry class, iterator moves next student next value hence when 2 students present nosuchelemennt exception.
public string format() { listiterator<student> iterator = studentlist.listiterator(); (int = 0; < studentlist.size(); i++) { system.out.println("if statement"); if (iterator.hasnext()) { system.out.println("hasnext"); return string.format("%0s%0s%0s", iterator.next().getsurname(), iterator.next().getforename(), iterator.next().getstudentid()); } } return null; }
i'd return 3 values stud1, move on stud2 , on.
here link student , registry source files , tester both; https://www.dropbox.com/sh/ol2jyfbuyyjnrcg/1j4lleaqc6
i post more code if it's requested.
since answer closed, no more code , files @ link have been removed.
to iterate around list without using iterator, should use enhanced loop. code becomes -
public string format() { listiterator<student> iterator = studentlist.listiterator(); (student student: studentlist) { system.out.println("for loop"); return string.format("%s%s%s", student.getsurname(), student.getforename(), student.getstudentid()); } return null; }
Comments
Post a Comment