1

I have an generic array(it keeps set of element and element of type is generic(maybe int,maybe char, maybe String etc.)

And also I think a set is sorted so I wanted to use bubblesort algorithm.But I took // bad operand types for binary operator '>' this error message.

What can I do for this sort function work some different data type(String,int,char,double)

This is my code :

void bubbleSort(T arr[]){
    int n = arr.length;
    for (int i = 0; i < n-1; i++){
        for (int j = 0; j < n-i-1; j++){
             if (arr[j] > arr[j+1]){  // bad operand types for binary operator '>'              
                  T temp = arr[j];
                  arr[j] = arr[j+1];
                  arr[j+1] = temp;
             }
         }
    }
}

Edit1:

If I tried compareTo:

void bubbleSort(T arr[]){
    int n = arr.length;
    for (int i = 0; i < n-1; i++){
        for (int j = 0; j < n-i-1; j++){
             if (arr[j].compareTo(arr[j+1])<0){  // bad operand types for binary operator '>'              
                  T temp = arr[j];
                  arr[j] = arr[j+1];
                  arr[j+1] = temp;
             }
         }
    }
}

The error message is :

cannot find symbol
  symbol:   method compareTo(T)
  location: class Object
  where T is a type-variable:
    T extends Object declared in class MySet

Edit2 : If I tried this:

<T extends Comparable<T>> void  bubbleSort(T arr[]) {
int n = arr.length;
for (int i = 0; i < n-1; i++){
    for (int j = 0; j < n-i-1; j++){
         if (arr[j].compareTo(arr[j+1])> 0) {
              T temp = arr[j];
              arr[j] = arr[j+1];
              arr[j+1] = temp;
         }
     }
}

}

When calling the function bubbleSort:

private T[] data;
bubbleSort(data);

The error message is :

method bubbleSort in class Set<T#2> cannot be applied to given types;
  required: T#1[]
  found: T#2[]
  reason: inference variable T#1 has incompatible bounds
    upper bounds: Comparable<T#1>
    lower bounds: T#2
  where T#1,T#2 are type-variables:
    T#1 extends Comparable<T#1> declared in method <T#1>bubbleSort(T#1[])
    T#2 extends Object declared in class Set
----
(Alt-Enter shows hints)

Edit3:

If I try this:

void bubbleSort(Comparable arr[]) {
    int n = arr.length;
    for (int i = 0; i < n-1; i++){
        for (int j = 0; j < n-i-1; j++){
             if (arr[j].compareTo(arr[j+1])> 0) {
                  Comparable temp = arr[j];
                  arr[j] = arr[j+1];
                  arr[j+1] = temp;
             }
         }
    }
}

And call the function bubbleSort :

   private T[] data;
    bubbleSort(data);

The error message is:

incompatible types: T[] cannot be converted to Comparable[]
  where T is a type-variable:
    T extends Object declared in class MySet
----
(Alt-Enter shows hints)
4
  • Use compareTo() instead Commented Dec 21, 2017 at 15:20
  • 1
    Make T implement Comparable<T> and use compareTo. Commented Dec 21, 2017 at 15:21
  • @FrankUnderwood I tried but I didn't work, see my edited post Commented Dec 21, 2017 at 15:25
  • @tarktark see the answers below. You need to bound the type parameter Commented Dec 21, 2017 at 15:30

4 Answers 4

3

You can't, unless you know that they're at least Comparable. You can't use inequality operators on an object, since they're not guaranteed to autobox to primtives.

At a minimum your generic type should be Comparable:

public class Sorter<T extends Comparable<T>> { }

...and you should look into compareTo, an exercise I leave for the reader.

Sign up to request clarification or add additional context in comments.

1 Comment

@tarktark: That's quite literally as specific as you need to get. Here's another hint: I didn't know what your class was called, so I went with Sorter. I can see from your stack trace that it's called GTUSet.
1

You can instead use

arr[j].compareTo(arr[j+1]) > 0

but this requires that T is Comparable, in which case the function signature should be like this:

void bubbleSort(Comparable arr[])

7 Comments

Would this be better practice then bounding the type parameter?
After that what should I do, can you write spesific code?
@tarktark I already wrote the two specific lines of code that should change. What else are you looking for?
Could you see edit3 in my post ? I write your lines of code but it doesn't work
@tarktark The data you are passing in is not Comparable. You need to declare private Comparable[] data;
|
0

The short answer - you can't. In order to be able to sort an array, you need the ability to compare between every two elements, which simply isn't possible with every class.

One option is to limit yourself to Comparable:

<T extends Comparable<T>> void  bubbleSort(T arr[]) {
    int n = arr.length;
    for (int i = 0; i < n-1; i++){
        for (int j = 0; j < n-i-1; j++){
             if (arr[j].compareTo(arr[j+1])) > 0 {
                  T temp = arr[j];
                  arr[j] = arr[j+1];
                  arr[j+1] = temp;
             }
         }
    }
}

Alternatively, you can allow the caller to supply their own Comparator:

<T> void  bubbleSort(T arr[], Comparator<T> cmp) {
    int n = arr.length;
    for (int i = 0; i < n-1; i++){
        for (int j = 0; j < n-i-1; j++){
             if (cmp.compare(arr[j], arr[j+1])) > 0 {
                  T temp = arr[j];
                  arr[j] = arr[j+1];
                  arr[j+1] = temp;
             }
         }
    }
}

4 Comments

for first option , how can I call this function?
@tarktark with any array of a class that implements Comparable. E.g.: String arr = new String[]{"stack", "overflow"}; bubbleSort(arr);.
@tarktark The T in T[] is not a Comparable in the snippet you shared.
@tarktark the same level that defines T[] (presumably the class) should define T as T extends Comparable<T>.
0

Here is a insertion sort that works for generics. I'm a java noob but I swear once I changed my .equals in the object I was using to test the generic class to correctly overload the object class it worked. Here is my .equals:

public boolean equals(Object obj) {
        if (obj != null && getClass() == obj.getClass()) {
            Transaction q = (Transaction)obj;
            return getValue() == q.getValue() && getDate().equalsIgnoreCase(q.getDate()) && getProvider().equalsIgnoreCase(q.getProvider());
        }
        return false;
    }

Check this link for a simpler example.

Here is my insertion sort. I'm just using the object from my lab I'm doing right now.

private void insertionSort(){

        T temp;
        for (int i = 1; i < numElements; i++) {
            for(int j = i ; j > 0 ; j--){
                if (((Comparable)list[j]).compareTo(list[j-1]) < 0) {
                    temp = list[j];
                    list[j] = list[j-1];
                    list[j-1] = temp;
                }
            }
        }

Please excuse my ignorance in terms of the .equals idea, but I just wanted to throw it out there. I was making a lot of changes and am probably just confused.

Also, I just declared the class public class MyArrayUnsortedList<T> rather than public class MyArrayUnsortedList<T extends Comparable<T>> if that makes a difference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.