JDK中Stack的bug
2017-04-01 22:52
176 查看
JDK中Stack的bug
在写 Dijkstra 的最短路径算法的时候,打印结果发现和预期的结果不同,查找出来的路径刚好是反着的。在经过多次bug排查后最后发现问题在路径的打印上面。Stack<DirectedEdge> path = new Stack<>(); for (DirectedEdge e = edgeTo[v]; e != null; e = edgeTo[e.from()]) path.push(e);
由于此处我使用了 Stack 去储存路径(得到路径的顺序本身是反着的),我以为直接 foreach 打印结果当然是从上到下的。可是并不是这样。通过下面代码我们就可以看出来:
public static void main(String[] args) { Stack<Integer> tmp = new Stack<>(); tmp.push(1); tmp.push(2); tmp.push(3); for (Integer e : tmp) System.out.println(e); }
是的,结果并不是3 2 1,而是1 2 3,这是为什么呢?
原因
是的,这是 JDK 的一个bug。在官方文档中,找到了回答:Java bugs官方文档,正如文中所说:It was an incorrect design decision to have Stack extend Vector (“is-a” rather than “has-a”). We sympathize with the submitter but cannot fix this becauseof compatibility.大意就是,是由于 Stack 继承自 Vector 类,所以用的是 Vector 的 iterator,这是他们设计的问题,但是由于兼容性问题,所以他们没办法解决。
解决办法
最后我使用了 LinkedList 来代替 Stack 使用,在源码中我们可以看到向 LinkedList 中插入元素有两中方法:/** * Appends the specified element to the end of this list. * * <p>This method is equivalent to {@link #addLast}. * * @param e element to be appended to this list * @return {@code true} (as specified by {@link Collection#add}) */ public boolean add(E e) { linkLast(e); return true; } /** * Pushes an element onto the stack represented by this list. In other * words, inserts the element at the front of this list. * * <p>This method is equivalent to {@link #addFirst}. * * @param e the element to push * @since 1.6 */ public void push(E e) { addFirst(e); }
也就是 add() 和 push()的区别,具体介绍源码已经写得很清楚了,头插和尾插的不同。这样一来,我们就可以安心的使用 foreach 来遍历得到结果了。
相关文章推荐
- JDK的一个关于stack的小bug
- java.lang.VerifyError: Expecting a stackmap frame at branch target JDK 1.7
- Bug:java.lang.StackOverflowError: stack size 8MB
- 转的一个jdk bug
- JDK1.6集合框架bug 6260652解析
- 深入jdk——追踪Collections.sort 引发的bug(1)mergeSort
- 启动Tomcat时报 Expected stackmap frame at this location.(JDK1.7编译)
- 使用两个Stack类(JDK容器类库中的Stack类)实现一个队列类MyQueue,提供队列的入队列和出队列操作:enQueue和deQueue
- jdk的Double.parseDouble漏洞bug~
- Bug in JDK
- jdk1.6.0_29的bug导致c3p0获取sqlserver连接时被hang住甚至deadlock问题的分析
- Eclipse解决bug:JDK是8.0,打不开eclipse
- famous summary stack trace from Oracle Version 8.1.7.4.0 Bug Note
- JDK学习-Stack/List
- jdk中的小bug(一)
- Linux内核调试之使用BUG_ON 和dump_stack
- JDK1.8下webservice的wsdl2java 命令出现的bug
- 117 FP页面无法查看 此错误是JDK8.0.0.0版本的一个BUG,会导致工单重复回写,
- 深入jdk——追踪Collections.sort 引发的bug(1)mergeSort
- 深入jdk——追踪Collections.sort 引发的bug(2)TimSort思路