【发布时间】:2018-05-27 16:53:31
【问题描述】:
我是 Java 新手,正在学习集合主题。谁能告诉我为什么在 ArrayDeque 和 Stack 中使用 push 方法时输出会发生变化?
ArrayDeque 示例程序:
public class Simple4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayDeque q = new ArrayDeque();
q.push("e");
q.push("f");
System.out.println(q);
}
}
输出: [f, e]
堆栈示例程序:
public class Simple5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack s = new Stack();
s.push("apple");
s.push("banana");
System.out.println(s);
}
}
输出: [苹果,香蕉]
【问题讨论】:
-
ArrayDeque 迭代器的排序(通过它的 toString() 方法访问是从头到尾(LIFO),使用 pop() 时得到相同的顺序
-
一般来说,如果您要问为什么两个代码 sn-ps 的行为不同,最好提供相同的输入。在这种情况下,添加相同的列表元素,以使差异可以直接比较。