Can I add to a generic collection of type A values of type B ,which extends A, without any special syntax?

前端 未结 3 1299
感情败类
感情败类 2021-01-22 10:06
public class Stack {
    public Stack () {....}
    public void push (E e) {....}
    public E pop () {....}
    public boolean isEmpty(){....}
}

public void p         


        
3条回答
  •  隐瞒了意图╮
    2021-01-22 10:44

    the problem is that you have two types but only one generic representation (E) so E is Number as well as Integer. Thats confusing him. You need to have the same type for the collection. Rewrite it to

    pushAll(Collection src) 
    

    and cast K to E.

提交回复
热议问题