Reputation: 126
I have the following simple method:
public static Stack transfer(Stack s) {
Stack t = new Stack();
return t;
}
I intend to fill in more functionality, but I'd like to make this applicable to generic stacks:
Stack<E>
Where E
is the generic type of the elements in the stack. I tried several ways to add the element to the Stack
, but I seem to keep getting errors no matter how I have them set up. I know this is a basic question, but I've never really used stacks before and I just need pointed in the right direction.
The code is in Java, using Eclipse. Stacks are found in java.util.Stack
Upvotes: 0
Views: 63
Reputation: 8386
You simply have to provide a type as type parameter to the already generic class Stack<T>
Stack<String> stringStack = new Stack<>();
Upvotes: 0
Reputation: 62884
The Stack class is already generic, but in order make the transfer
method generic, you have to do:
public static <E> Stack<E> transfer(Stack<E> s) {
Stack<E> t = new Stack<E>();
return t;
}
Upvotes: 3