So, I am working on a program that includes two types of data: a linked list and an Arraylist.
The linked Iterator list looks like this:
private class NodeIterator implements Iterator<StudentIF> {
private Node curr;
public NodeIterator(Node head) {
curr = head;
}
public void remove() { }
public boolean hasNext() {
if (curr == null)
return false;
return true;
}
public StudentIF next() {
Node temp = curr;
curr = curr.getNext();
return temp.getData();
}
}
and I call the ArrayList Iterator method / class.
MyArrayListName.iterator();
Here's a method that does the job of calling iterators:
public StudentIF getStudent(int id) {
Iterator<StudentIF> xy = iterator();
while (xy.hasNext()) {
if (id == xy.next().getId()) {
return xy.next();
}
}
return null;
}
My problem is that when I call my methods to get my object by their identifier (instance variable), it always captures the NEXT object, and not the object that I want. How to get the current object with both a Linked List and an array?
Please help me!
source
share