search

Home  >  Q&A  >  body text

反射 - java ,将 object类型转换为 ArrayList<> 类型?

伊谢尔伦伊谢尔伦2767 days ago686

reply all(5)I'll reply

  • 阿神

    阿神2017-04-17 17:00:01

    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?

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:00:01

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

    reply
    0
  • PHPz

    PHPz2017-04-17 17:00:01

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:00:01

    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);//报错
        }
    }

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:00:01

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

    reply
    0
  • Cancelreply