Pregunta

So I've been trying to get the average of my integer array and my method down there isn't doing the greatest because I'm trying to fit an object reference into a primitive.. I tried using intValue and I think im using it wrong.. any ideas?

       //Variation 3 - Choose Mean as pivot
   private static int getMeanIndexAsPivotIndex(Comparable comparableArray[],int leftIndex, int rightIndex) {
          int sum = 0;
          int add = 0;
    for (int i = 0; i < comparableArray.length; i++) {
        add = comparableArray[i].intValue();
        sum +=  add;
    }
    return sum / comparableArray.length;
}

Here is where I made my array

     //Random Array
 Integer[] unsortedArray = new Integer[8];
   for (int i = 0; i < unsortedArray.length; i++) {
       unsortedArray[i] = randomRange(0,1000);
   }


    QuickSort.java:157: error: cannot find symbol
        add = comparableArray[i].intValue();
                                ^
  symbol:   method intValue(int)
  location: interface Comparable
Note: QuickSort.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
¿Fue útil?

Solución

According to your signature comparableArray is of type Comparable[] instead of an Integer[], this type does not have an .intValue() method.

Changing the method signature to use Integer[] should solve the problem.

Otros consejos

Elaborating on @EricBouwers's answer.

The class Integer extends the class Number and implements the Comparable interface, i.e., the class declaration is

public final class Integer extends Number implements Comparable<Integer>

Implementing Comparable in a class means that the class provides a means (a method) to compare between different instances of the class using the compareTo method. This method is (the only one) declared in the Comparable interface and implemented in the classes implementing the interface.

This has nothing to do with the intValue method of class Integer. This method is declared and implemented in class Number and overridden by its subclass Integer. It unboxes an Integer object (instance of the class) to an int primitive.

As for your code, you are trying to call intValue for the Comparable interface, but is is undefined there, hence the error: cannot find symbol (symbol being the method you tried to invoke). You are confused because Integer "includes" Comparable, but it doesn't work the other way around.

You can write this:

Comparable<Integer>[] arr = new Integer[]{1,2,3};

but then arr will not be able to invoke intValue because it is of type Comparable and not Integer. It will be able to invoke compareTo.

You cannot write this:

Integer[] arr = new Comparable<Integer>[]{1,2,3};

although in theory you might think that you will be able to invoke both intValue and compareTo on arr.

What you need to do is this:

Integer[] arr = new Integer[]{1,2,3};

and then you can invoke both intValue and compareTo on arr.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top