0

How I can avoid generic warnings in the following code? Eclipse Generic Warning : Type safety: Unchecked cast from T to E

Is there a way to improve this code. Just putting and removing values from Stack.

public static <T, K, E> void genericStack(T a, K b) {
    Stack<E> st = new Stack<E>();
    st.push((E) a);
    st.push((E) b);
    b = (K) st.pop();
    a = (T) st.pop();
}
David
  • 504
  • 5
  • 21
  • What is the purpose of the `T` and `K` parameters? – Lee Apr 14 '15 at 20:59
  • T and K can be anything like Integer, String, which can be pushed into the stack. for example: – David Apr 14 '15 at 21:04
  • If `T` and `K` can be anything then you don't need to use generics and you can just use `Object`. There's no way to make this safe if there's no relationship between the types you're using. – Lee Apr 14 '15 at 21:08

0 Answers0