尝试使用堆栈识别回文时出现 <identifier> 错误?

我正在做一项识别回文的任务,我必须使用堆栈来完成它。我目前收到消息错误:预期标识符:


palStack.push(nextChar);


我的教授说我正在尝试将 Stack 用作原始类型,这就是我收到错误的原因,但我不确定他到底是什么意思?我对数据结构很陌生,所以任何关于使用 Stack 的提示都将不胜感激!


这是我遇到问题的部分:


public static void main(String[] args){

    int replay = 0; 

    Stack<char> palStack = new Stack<>(); 

    char nextChar; 

    int characterCount;

    String phrase, emptyPhrase = "", replayAns; 

    @SuppressWarnings("unchecked")

    Scanner reader = new Scanner(System.in); 


    while(replay != 1){

        System.out.println("Enter phrase: ");

        phrase = reader.nextLine(); 

        characterCount = phrase.length(); 


        System.out.print("Original phrase: "); 


        for(int i = 0; i < characterCount; i++){

            nextChar = phrase.charAt(i); 


            @SuppressWarnings("unchecked")

            palStack.push(nextChar);    

            System.out.print(nextChar); 

        }

    }

}


守候你守候我
浏览 67回答 1
1回答

有只小跳蛙

问题出在这一行:Stack<char>&nbsp;palStack&nbsp;=&nbsp;new&nbsp;Stack<>();您不能将原语/原始类型(在这种情况下char)用作泛型类型(https://docs.oracle.com/javase/tutorial/java/generics/types.html)。所以你只需要使用一个引用类型,即java.lang.Character:Stack<Character>&nbsp;palStack&nbsp;=&nbsp;new&nbsp;Stack<>();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java