简体   繁体   中英

Converting from Integer wrapper class to int primitive class

I've been trying to convert an Integer Wrapper class to int primitive class. I haven't yet found a proper way to make the code compile. I'm using Intellij IDEA, Java 11 Amazon Coretto, but I need to run it on a computer that runs java 8.

Here's the original code below:

static class Line<Integer> extends ArrayList<Integer> implements Comparable<Line<Integer>> {

        @Override
        public int compareTo(Line<Integer> other) {
            int len = Math.min(this.size(), other.size());
            for (int i = 0; i < len; i++) {;
                if ((int) this.get(i) != (int) other.get(i)) {
                    if ((int this.get(i) < (int) this.get(i)) {
                        return -1;
                    } else if ((int) this.get(i) > (int)this.get(i)) {
                        return 1;
                    } else {}
                }
            }
    ...

note that the Line is inserted to an ArrayList.

Originally I used forced casting on all the Integer objects so it'll be like (int) this.get(i) . It worked on my local terminal and my Intellij wasn't bothered about it, but unfortunately not the other computer. It couldn't compile there

I thought it was because of the forced casting, since the other computer returned

Main.java:159: error: incompatible types: Integer cannot be converted to int
if ((int) this.get(i) != (int) other.get(i)) {
^
where Integer is a type-variable:
Integer extends Object declared in class Line

so I deleted them all and thought I could let the machine unbox the Integer wrapper on its own. It still didn't compile.

If the code is left like what's written above (no forced casting), it will return "Operator '<' not applicable for 'Integer', 'Integer'" So I used the.compareTo() method. Compile error.

Then I tried to assign them to an int variable. Intellij IDEA was screaming at me that it required int but found Integer instead. So I force-casted, like so

int thisLine = (int) this.get(i);
int otherLine = (int) other.get(i);
if (thisLine != otherLine) {
    if (thisLine < otherLine) {
        return -1;
    } else if (thisLine > otherLine) {
        return 1;
    } else {}

Nope, didn't work. Removing the cast also didn't work.

I looked up the Javadocs ( https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#intValue-- ) this time about the Integer class and found a promising little method called intValue(). Problem is? Intellij cannot resolve that method (oddly, VSCode does not consider this an error). I used it like this

int thisLine = this.get(i).intValue();
int otherLine = other.get(i).intValue();
if (this.get(i) != other.get(i)) {
    if (thisLine < otherLine) {
        return -1;
    } else if (thisLine > otherLine) {
        return 1;

and sure enough, another compile error on that stubborn computer.

I'm running out of options. I'm seriously considering creating a new custom class just so I can store int values in an ArrayList without having to deal with all this Java backwards incompatibility nonsense. Anyone here know a consistent method for converting an Integer wrapper object to an int primitive object in Java?

This is the clue in the error message that explains it:

where Integer is a type-variable:

Integer extends Object declared in class Line

Integer is not java.lang.Integer but a type variable with a confusing name...

You declared the type variable here:

static class Line<Integer> extends ArrayList<Integer> implements Comparable<Line<Integer>>

It's as if you declared it like this:

static class Line<T> extends ArrayList<T> implements Comparable<Line<T>>

but by naming the type variable Integer instead of T , and then you try to cast objects of the type T to int later on.

Fix it by not declaring a type parameter named Integer , you don't need that here:

static class Line extends ArrayList<Integer> implements Comparable<Line<Integer>>

You shouldn't have to cast an Integer to an int at all. Integer class has .compareTo methods which compare two integers.

A 0 means value1 is equal to value2. -1 is value1 is less than value2 and a 1 is value1 is greater than value2.

Try the following:

public int compareTo(Line<Integer> other) {
  //get the smallest length
  int len = this.size() <= other.size() ? this.size() : other.size();
  for (int i = 0; i < len; i++) {
    int compare = this.get(i).compareTo(other.get(i));
    if (compare != 0) { //if compare is not zero they are not the same value
       return compare;
    }
  }
  //If we get here, everything in both lists are the same up to "len"
  return 0;
}

The compareTo() method is a method of Integer class under java. lang package. ... It returns the result of the value 0 if Integer is equal to the argument Integer, a value less than 0 if Integer is less than the argument Integer and a value greater than 0 if Integer is greater than the argument Integer.

In you class "Integer" is not a java.lang.Integer but a Generic class that is the reason

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM