簡體   English   中英

java.util.Stack 有什么問題?

[英]What's wrong with java.util.Stack?

使用Stack s = new Stack()初始化基本堆棧中沒有實際方法嗎? 不存在pushpop方法嗎?

按照這個基本教程,錯誤消息說The method push(Integer) is undefined for the type Stack 例如這樣的基本操作:

import java.util.*;

public class StackOfPlates {

    public static void main(String[] args) {
        Stack s = new Stack();
        s.push(new Integer(6));
    }
}

拋出未定義的函數錯誤。 我錯過了一些明顯的東西嗎?

我可能會使用像ArrayDeque這樣的Deque ,並且不要使用原始類型

Deque<Integer> s = new ArrayDeque<>();
s.push(1);
s.push(2);
s.stream().forEachOrdered(System.out::println);

哪個輸出2然后1。

如果您將其更改為使用Stack ,則是相同的基本問題 - 不要使用原始類型。 它應該看起來像,

Stack<Integer> s = new Stack<>();
s.push(1);
s.push(2);
s.stream().forEachOrdered(System.out::println);

這給出了相同但相反的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM