【发布时间】:2021-02-06 06:43:34
【问题描述】:
我想在下面的代码执行后输出每个堆栈的内容。到目前为止它的输出为
Stack@568db2f2
Stack@378bf509
Stack@378bf509
但我想要每个堆栈的内容。
代码:
public static void main(String[] args) {
Stack<Integer> a = new Stack<>();
Stack<Integer> b = new Stack<>();
Stack<Integer> c = new Stack<>();
a.push(28);
b.push(a.pop());
b.peek();
c.push(21);
a.push(14);
a.peek();
b.push(c.pop());
c.push(7);
b.push(a.pop());
b.push(c.pop());
System.out.println(a);
System.out.println(b);
System.out.println(b);
}
【问题讨论】:
-
如果您不需要在程序结束时仍然填充堆栈,您可以
pop一次一个元素(直到堆栈为空)并打印它。 -
您期望每个堆栈内容的输出类型是什么?
String[28, 21, 14, 7] ? -
@aKilleR 他们最终会是空的吗?
-
@EGS99 ,如果使用
peek()不会为空检查This out