简体   繁体   English

Java:迭代器

[英]Java: Iterators

So I'm working on a program that involves two datatypes: a linked list and a Arraylist. 所以我正在开发一个涉及两种数据类型的程序:链表和Arraylist。

The linked List Iterator looks like: 链接的List Iterator如下所示:

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();
        }

    } // end class NodeIterator

and I call the ArrayList Iterator method/class. 我调用ArrayList Iterator方法/类。

MyArrayListName.iterator();

Here's the method that does the work of calling the iterators: 这是调用迭代器的方法:

public StudentIF getStudent(int id) {
    Iterator<StudentIF> xy = iterator();
    while (xy.hasNext()) {
        if (id == xy.next().getId()) {
            return xy.next();
        }
    }
    // Student doesn't exist
    return null;
}

My problem is when I call my methods to get my object by their id(instance variable), it always grabs the NEXT object, not the object I want. 我的问题是当我调用我的方法通过它们的id(实例变量)获取我的对象时,它总是抓取NEXT对象,而不是我想要的对象。 How do I get the current object with both the Linked List and the Array list? 如何使用链接列表和数组列表获取当前对象?

Please help me! 请帮我!

You use the next() method twice, that's probably why. 你使用next()方法两次,这可能就是原因。

Try this 尝试这个

  while (xy.hasNext()) {
        StudentIF tmp = xy.next();
        if (id == tmp.getId()) {
            return tmp;
        }

The problem is that you're calling .next() twice in your loop here: 问题是你在循环中调用.next()两次:

if (id == xy.next().getId())
{
    return xy.next();
}

Calling next() twice will advance your iterator twice which isn't what you want. 两次调用next()会使你的迭代器前进两次,这不是你想要的。 You need to save the next off in a temporary variable like this: 您需要将下一个保存在临时变量中,如下所示:

StudentIF nextStudent = xy.next();
if (nextStudent.getId() == id)
{
    return nextStudent;
}

Everytime you use the next() method it increments the iterator, so by calling 每次使用next()方法时,它都会递增迭代器,因此通过调用

if (id == xy.next().getId())

and

return xy.next();

you're actually incrementing the iterator. 你实际上在增加迭代器。

Your best bet is to store xy.next(), make any comparisons you need and then return it as follows: 您最好的选择是存储xy.next(),进行所需的任何比较,然后按如下方式返回:

public StudentIF getStudent(int id) {
Iterator<StudentIF> xy = iterator();
while (xy.hasNext()) {
    StudentIF student = xy.next();
    if (id == student.getId()) {
        return student;
    }
}
// Student doesn't exist
return null;

} }

You are calling .next() twice. 你正在两次调用.next()

The solution should be calling it only once and saving it in a variable like this: 解决方案应该只调用一次并将其保存在这样的变量中:

 while (xy.hasNext()) {
        StudentIF student = xy.next();
        if (id == student.getId()) {
            return student;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM