簡體   English   中英

如何引用迭代器中的當前對象

[英]How do I refer to the current object in an iterator

我試圖在TreeSet中實現一個搜索方法。 通過使用帶有condtional的迭代器,我希望能夠遍歷集合並打印匹配條件的對象。 然而,我現在這樣做的方式是打印出后續對象而不是當前對象。 這是我到目前為止:

public void getDetails() {
        Iterator<Person> it = this.getPersonSet().iterator();
        System.out.println("Enter First Name");
        String first = in.next().toLowerCase();
        System.out.println("Enter Second Name");
        String last = in.next().toLowerCase();

        while (it.hasNext()) {
            if (it.next().getLast().toLowerCase().equals(last)) {
                Person p = it.next();
                System.out.println(p);
            }
        }

    }

任何幫助都會很棒

這是你想要做的:

while (it.hasNext()) {
            Person p = it.next();
            if (p.getLast().toLowerCase().equals(last)) {
                System.out.println(p);
            }
        }

如何引用迭代器中的當前對象

對於記錄, Iterator API不允許您這樣做。 沒有“當前”對象的概念。 Iterator.next()方法為您提供下一個對象...並繼續前進。

ListIterator.previous()ListIterator.next()方法是類似的。請注意,在ListIterator情況下,方法行為是根據游標來記錄的,該游標表示迭代序列中元素之前/之間/之后的位置。)

解決方案是將調用it.next()的結果分配給臨時變量,如接受的答案所述。


我不確定為什么設計師沒有在API中包含“當前”對象的概念,但我可以想到幾個原因:

  • 它會使典型的迭代器對象更大; 即一個額外的字段來保存當前對象。
  • 這意味着要為Iterator類實現更多的方法。
  • 當前對象的概念不適合ListIterator接口中記錄的“游標”模型......並且由當前的Iterator設計暗示。
  • 迭代器“掛在”當前對象上有一個小問題,從而阻止它被GC控制。
  • 絕大多數迭代器用例不需要當前對象。
  • 還有其他方法可以解決這個問題。

聽起來很不錯......

如果您需要現有的實現,可以使用Google GuavaApache Commons Collections中的實現
其他答案對於您的簡單問題更容易,但如果您需要傳遞迭代器並跟蹤next()返回的最后一項,這些將有所幫助。

下面是一個使用Guava和OP代碼的例子(假設Person確實有一個String toLowerCase()方法):

import com.google.common.collect.PeekingIterator;
import static com.google.common.collect.Iterators.peekingIterator;

public void getDetails() {
    PeekingIterator<Person> it = peekingIterator(this.getPersonSet().iterator());
    System.out.println("Enter First Name");
    String first = in.next().toLowerCase();
    System.out.println("Enter Second Name");
    String last = in.next().toLowerCase();

    while (it.hasNext()) {
        // note the usage of peek() instead of next()
        if (it.peek().getLast().toLowerCase().equals(last)) {
            Person p = it.next();
            System.out.println(p);
        }
    }

}

在單獨的var中保持對象的引用:

Person current = it.next();
current.methodOne();
current.methodTwo();

完成當前值后,再次對其進行重新分配

...
// done? 
current = it.next();

在循環中看起來像:

while( it.hasNext() ) { 
   Person current = it.next();
   current.doA();
   current.doB();
   current.doC();
 }

next()方法返回當前對象,如下所示:

private class IterSinglyLinked implements SimpleIterator<T> {
    Element curr = head;        // next element to return

    public boolean hasNext() {
        return curr != null;
    }

    public T next() throws Exception {
        if (curr == null) throw new Exception("no more elements");
        T data = curr.data;
        curr = curr.next;
        return data;
    }
}

如果它返回下一個而不是當前的那個,則無法到達第一個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM