java.util.Stack的Iterator中是否有错误? [英] Is there a bug in java.util.Stack's Iterator?

查看:155
本文介绍了java.util.Stack的Iterator中是否有错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我试图推入 java.util.Stack 类,然后使用 Iterator 来迭代(没有使用流行)通过项目。我期待着LIFO的财产,但感到惊讶。

Today I was trying to push in java.util.Stack class and then use the Iterator to iterate (without using pop) through the items. I was expecting LIFO property but got surprised.

这是我正在尝试的代码。

Here is the code that I was trying.

import java.util.*;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        RobStack<Integer> rstack = new RobStack<Integer>(); // Correct Implementation
        Stack<Integer> jstack = new Stack<Integer>(); // Default Java Implementation
        rstack.push(0); jstack.push(0);
        rstack.push(1); jstack.push(1);
        rstack.push(2); jstack.push(2);
        rstack.push(3); jstack.push(3);

        System.out.print("Algo Stack: ");
        for (int i : rstack)
            System.out.print(i + " ");
        System.out.print("\nJava Stack: ");
        for (int i : jstack)
            System.out.print(i + " ");
    }

}

给出上述程序的输出以下:

The output the above program is given below:

Algo Stack: 3 2 1 0 
Java Stack: 0 1 2 3 

在上面的代码中 jstack 使用默认的Java实现和 rstack 使用由Robert Sedgewick提供的实现为他的算法类。我发现Robert教授的实现工作正常,但$ java.util.Stack 实现失败。

In the above code jstack uses the default Java implementation and rstack uses the implementation provided by Robert Sedgewick for his Algorithm class. I found that Prof. Robert's implementation works fine but the java.util.Stack implementation fails.

这是一个错误还是通过设计

推荐答案

请参阅错误ID 4475301:RFE:java.util .Stack.iterator()重复错误的方式。这种行为是(坏)的设计。 Java内置的 堆栈 迭代器方法是从其他类继承的,所以它们的行为不如预期的那样。

See Bug ID 4475301 : RFE: java.util.Stack.iterator() iterates the wrong way. This behavior is by (bad) design. Java's built-in Stack iterator methods are inherited from other classes, so they don't behave as you'd expect.

这篇关于java.util.Stack的Iterator中是否有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆