Rumah > Soal Jawab > teks badan
PHPz2017-04-17 17:00:01
Jibing m = (Jibing)list.get(i);这句前面的强转是多余的,list本身存的就是就是Jibing。报错应该就是ClassCastException,那么问题就是出在了ArrayList<Jibing> list =(ArrayList<Jibing>)date;这句,说明date是一个集合,但是集合里面不是Jibing,所以你可以强转成ArrayList,但是当你获取里面的元素时就不能强转成Jibing了。
PHP中文网2017-04-17 17:00:01
Object date=pagelist.getResult();
返回类型要明确,不要强转,鬼知道返回的类型是什么
使用泛型吧
public class ServiceResult<T>{
private String resultMsg="";
private int resultCode="";
private T result;
public T getResult(){
return result;
}
public void setResult(T returnObj){
this.result=returnObj;
}
}
...
ServiceResult<ArrayList<Jibing>> date=pagelist.getResult();
ArrayList<Jibing> list = date.getResult();
if (list != null && list.size() > 0)
{
for (int i = 0; i < list.size(); i++)
{
Jibing m = (Jibing)list.get(i);//报错
}
}