سؤال

I'm currently working on a University assignment. I have a piece of code that looks like the following.

if(temporary > left) { }

The temporary and left variables are both of the "Comparable" type. My question is, how can I compare these two? They always hold integer values, but the assignment forces me to declare them as "Comparable" types.

Any help is much appreciated as I'm quite confused with this. Thanks!

هل كانت مفيدة؟

المحلول

If you have a look at http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html, you will know that the compareTo() method is what you need.

You can use temporary.compareTo(left) and use the value to compare with 0.

Implement compareTo() in temporarytemporary.compareTo(left) such that it returns a negative integer, zero, or a positive integer as temporary is less than, equal to, or greater than the left.

نصائح أخرى

In Java, interfaces can only declare methods. The Comparable interface declares the compareTo method. So:

temporary.compareTo(left)

Consider the following class as an example:

public class Boot implements Comparable<Boot> {

    int size;

    public Boot(int size) {
        this.size = size;
    }

    public int getSize() {
        return size;
    }

    @Override
    public int compareTo(Boot boot) {
        if (boot.getSize() > size) {
            return -1;
        } else if (boot.size == size){
            return 0;
        } else {
            return 1;
        }
    }

}

I had to write a program for my java class as well using comparable datatypes, it didn't really explain what a comparable datatype is exactly, but this is how I used it to do an insertion sort on an array of Strings which are objects that implement comparable interface. My understanding is that you can use the comparable datatype to point to an object which implements the comparable interface, which I used below to temporarily hold a value.

/* This class implements a method that can sort string objects in an array. using the insertion sort method. Our Main Method will call the ObjectInsertionSorter Method. */

public class Comparable {

public static void main(String[] Args){

    /*
    Create an array of strings to use as a test for the program.
    */
    String[] values = {"Dylan", "Daniel", "Michael", "Amanda", "Mitsy", "Sasha", "Carlos"};

    /*
    Call our method and pass it the values array.
     */
    objectInsertionSorter(values);

    /*
    Display array values.
     */
    for (String element : values){
        System.out.print(element + " ");
    }
}

public static void objectInsertionSorter(Comparable[] array){
    Comparable unsortedValue; // Temporarily holds our unsorted value
    int scan; // used to scan the array;

    /*
    This for loop steps through our array, starting at the second value.
     */
    for (int index = 1; index < array.length; index++){
        unsortedValue = array[index];
        scan = index;

        /*
        This while loop compares current value to the previous value.
        If the previous value is larger, then the current value is set equal to the previous one,
        and our current index "scan" is decremented.
        If the previous value is smaller in comparison. Then the previous value is set equal to the unsorted value.
        This will insert the unsorted value to it's correct position.
         */
        while(scan > 0 && array[scan - 1].compareTo(unsortedValue) > 0){
                array[scan] = array[scan - 1];
                scan--;
        }
        // sets current value to the unsorted value
        array[scan] = unsortedValue;
    }

}

}

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top