Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can i cast Integer to int? I was working in eclipse and the casting to (int)

ID: 3863942 • Letter: H

Question

How can i cast Integer to int?

I was working in eclipse and the casting to (int) worked.

but when i changed it into eclipse it says I can't cast Integer into int.

So I deleted the (int) and it says I cannot compare Object with Object

the given arraylist is

private Integer[]theItems;

sorting array public void sort int j boolean flag true set flag to true to begin first pass Integer temp //holding variable while flag flag false //set flag to false awaiting a possible swap for j 0; size -1: j++ if int) the Items [j]> (int) the Items [j+l] temp the Items the Items [j] the Items [j+1] the Items [j+1] temp //change element order flag true

Explanation / Answer

We can't direct typecast Integer object to int directly like above in the program I.e.(int)theItems[i].

To convert Integer object to int value we have to use Wrappers class concept.

I.e. to convert Integer object to int we have to use intValue() method.

For example, Integer i = new Integer (10);

int intValue =i. intValue();

In the above example try to use like this

if (theItems[j].intValue())>(theItems[j+1].intValue())

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote