auto binding (type inference) of generic types by the compiler

后端 未结 5 686
梦毁少年i
梦毁少年i 2021-01-24 01:11

the following code has compilation error in the line of t3:

public  List getList()
{
    return new ArrayList();
}
public  vo         


        
5条回答
  •  野性不改
    2021-01-24 01:44

    Try casting it to the type T.

        public  List getList()
        {
            return new ArrayList();
        }
        public  void first()
        {
            List ret = new ArrayList();
            List list = getList();
            T t1 = ret.get(0);
            T t2 = list.get(0);
            T t3 = (T) getList().get(0);
        }
    

提交回复
热议问题