-2
static ArrayDeque<Integer> bfs = new ArrayDeque<Integer>();
static Iterator<Integer> revbfs = new ArrayDeque<Integer>();

revbfs=bfs.descendingIterator();

非常快速的问题。我想以相反的顺序返回 bfs。有 descendingIteartor 方法,我只是不确定它返回的是什么。在这种情况下 revbfs 会是什么?我如何使用该方法来获得我的反向订单?

4

1 回答 1

0

好吧,这很容易用这段代码测试:

public class Test {
    static Deque<Integer> bfs = new ArrayDeque<>();

    public static void main(String[] args) {
        bfs.add(1);
        bfs.add(2);
        bfs.add(3);

        Iterator<Integer> revbfs = bfs.descendingIterator();
        while(revbfs.hasNext()) {
            System.out.println(revbfs.next());
        }
        // output is 3, 2, 1
    }

}

此外,官方文档可能会很有帮助,在这种情况下:

迭代器降序Iterator()

以相反的顺序返回此双端队列中元素的迭代器。元素将按从最后(尾)到第一个(头)的顺序返回。

每当您不确定某些类/方法的行为时,请尝试搜索、阅读和测试。

于 2017-11-03T10:52:14.223 回答