ClassCastException : cant be cast to java.lang.Integer

前端 未结 4 668
谎友^
谎友^ 2021-01-07 14:20

I know this has been asked a lot in stackoverflow but I could not find answers that work to my problem.

In the following code below, I cant print out each item in

4条回答
  •  遥遥无期
    2021-01-07 14:49

    You declare:

    int from;
    int to;
    int type;
    

    and you should declare them as Integers instead:

    Integer from;
    Integer to;
    Integer type;
    

    because later on you're doing:

    this.from = new Integer(from);
    

    etc.

    A better option would be to change the assignment to:

    this.from = from;
    

    which would also solve this error since you would be assigning an int to an int. Is there a purpose you're using new Integer() ? because if not - I would suggest removing it - it's slower (performance-wise) comparing to the primitive int.

    Bottom line: stay consistent and either use int throughout the code, or Integer - try not to mix them unless it's really required.

提交回复
热议问题