4

Beginner, can't seem to wrap my head around this.

Item Class

public class Item implements Comparable {

private int number;
private String description;

public Item(int num, String descript){
    this.number = num;
    this.description = descript;
}

public int getNumber(){
    return number;
}

public String getDescription(){
    return description;
}

@Override public int compareTo(Object o){
    Item i = (Item) o;
    if(this.getNumber() < i.getNumber())
        return -1;

    if(this.getNumber() > i.getNumber())
        return 1;

    return 0;
}

}

Main method

Item[] items = new Item[3];
    items[0] = new Item(102, "Duct Tape");
    items[1] = new Item(103, "Bailing wire");
    items[2] = new Item(101, "Chewing Gum");

    Arrays.sort(items);

    for (Item i : items){
        System.out.println(i.getNumber() + ": " + i.getDescription());
    }

When the main method instantiate items[0] = new Item(102, "Duct Tape"); Which goes through Item constructor to set current variable number, description.

My problem is I can't seem to understand what is being passed in the compareTo argument, therefore I can't seem to understand this.getNumber() < i.getNumber() is doing...

Return a negative number if current object is less than passed object?

any help is much appreciated.

4

4 回答 4

2

The raw interface Comparable that you are using allows this object to be comparable to another Object. It is then cast to Item to ensure that the this Item is only compared to another Item.

According to the Comparable Javadocs,

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Additionally, your compareTo will throw a ClassCastException at runtime if the Object isn't an Item.

It's better to use the generic form of the interface:

public class Item implements Comparable<Item> {

Then you can specify Item instead of Object to compare against:

@Override public int compareTo(Item i){

and you don't have to cast the Object to an Item. When calling compareTo, the compiler will enforce that the object to compare to must be an Item.

于 2013-07-16T23:47:26.483 回答
1

My problem is I can't seem to understand what is being passed in the compareTo argument

Array.sort uses the compareTo method to know how to compare pairs of items from the array.

Your sort method assumes that it will only be called with instances of Item, and that's all Array.sort will pass along to compareTo assuming that your array only holds instances of Item.

You will get a runtime exception if compareTo is ever invoked with the item being compared to not being an instance of Item.

I can't seem to understand this.getNumber() < i.getNumber() is doing...

It's comparing two instance of Item based on the value of getNumber, that is, the value of their number fields. It assumes that two instances of Item are equal if Item.number is the same for each instance, otherwise one is less than the other if its Item.number is less than the other's Item.number.

于 2013-07-16T23:49:23.790 回答
0

You are trying to override the default compareTo() method, which takes in a single Object as a parameter. Thus, in your implementation, the parameter needs to be of type Object. However, you assume that you will be passing in a Item object, so you can cast it accordingly.

So in the end, your Item object is treated like an Object, which is casted back to an Item in your compareTo() method.

于 2013-07-16T23:49:36.237 回答
0

The Object being passed to your compareTo method is an Item object. The Arrays.sort() calls the Overrided method method when you have it implemented, the best implementation is to return the compareTo method for the primitive java types

 @Override 
 public int compareTo(Object o){
      return (this.getNumber().compareTo((Item)o.getNumber());
 }

(this requires you to have number as type Integer though)

于 2013-07-16T23:56:55.893 回答