The type Stack is not generic; it cannot be parame

2019-03-06 18:20发布

问题:

I am trying to write a simple program to use stacks.It is giving me the error

The type Stack is not generic; it cannot be parameterized with arguments

import java.util.*;

    public class Stack {
            public static void main(String[] args)
            {
                Stack<Character> stack = new Stack<> ();
                s.push("Hello");
                System.out.println(s);

            }

        }

回答1:

Your class Stack is shadowing java.util.Stack. You could rename your class, or use the fully qualified class name like

java.util.Stack<Character> stack = new java.util.Stack<> ();


回答2:

Or you can import java.util.Stack with the other imports to avoid such type of error.