java如何存储list数据?
PHPz
PHPz 2017-04-17 18:01:21
0
3
417

我想从数据库中取出一组数据保存到List中,然后根据我自己的业务处理之后,再把该List保存到另一个List中去,比如说arlist..应该如何操作?

以下是我的代码。。但是报错了。

private List<TPhoneAttribute> temp;
private PhoneAttributeService PhoneAttributeService;
List listResult = null;
for(int i =0;i < attrList.size();++i){
            temp = PhoneAttributeService.sortByGoodsId(attrList.get(i).getType(), attrList.get(i).getGoodsId());
            listResult.add(temp);
        }
        

报错如下:
java.util.ArrayList cannot be cast to com.ones.domain.TPhoneAttribute..

我猜应该是类型不匹配。。请问如何才能达到我以上所描述的情况?

PHPz
PHPz

学习是最好的投资!

Antworte allen(3)
大家讲道理

Collections.copy()

洪涛
cannot be cast to  这个已经给出了你,一个错误提示。就是类型转换错误。你把一个A类型的数据转换成B类型,也没有做强制转换处理,A类和B类也没有直接或者间接的集成关系,因此会抛异常啊。
洪涛

listResult的类型不对,而且你的listResult都还没初始化呢,直接调用add()方法会报空指针异常的,所以应该是

//或者其他List
List<List<TPhoneAttribute>> listResult = new ArrayList<>();
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage