list添加值,试过很多,都不可以


List<CommunityComTm> list= null;

上面这这个list中我要添加下面两个值,
userDto.getCountyId()
userDto.getCountyname()
但是我用
list.add(userDto.getCountyId())
不行,这个是为什么

要有实现类

没有初始化

List<CommunityComTm> list = new ArrayList();

首先 list没有实例化,null是不能调用方法,会报空指针的错误。需要先初始化,new ArrayList();
其次,List定义里面存的类型是CommunityComTm,如果放入其它类型,就会报错

list是接口,得用它的实现类

List是接口,要实例化实现List接口的类