反射 - java ,将 object类型转换为 ArrayList<> 类型?
伊谢尔伦
伊谢尔伦 2017-04-17 16:58:01
0
5
684
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(5)
阿神

Force transfer is easy to report errors, you need to make sure the data is suitable for forced transfer
What specific function is to be achieved?

巴扎黑

list.get() put parentheses, the execution order is obviously wrong.

PHPzhong

Jibing m = (Jibing)list.get(i); The strong transfer in front of this sentence is redundant, the list itself stores Jibing. The error reported should be ClassCastException, then the problem lies in the sentence ArrayList<Jibing> list =(ArrayList<Jibing>)date;, which shows that date is a set, but the set is not Jibing, so you can force it to be converted to ArrayList, but when When you get the elements inside, you can't force it to Jibing.

Ty80

Object date=pagelist.getResult();
The return type must be clear, don’t force it, who knows what the return type is
Use generics

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);//报错
    }
}
左手右手慢动作

It depends on what type your date is finally returned, and convert it according to the type.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!