compareTo方法特许经营类

时间:2014-03-12 02:44:01

标签: java class methods arraylist compareto

package prob1;

public class Franchise implements Comparable<Franchise> {
    private String location;
    private double totals = 0;
    private String owner;

    public Franchise(String location,double totals,String owner){
        this.location = location;
        this.totals = totals;
        this.owner = owner;
    }
    public String getLocation(){
        return location;
    }
    public double getTotal(){
        return totals;
    }
    public String getOwner(){
        return owner;
    }
    public int compareTo(Franchise that) {
        int stateComparition = this.getLocation().compareTo(that.getLocation()); 
        Double sales = Double.valueOf(this.getTotal());    
        Double thatSales = Double.valueOf(that.getTotal());
        int salesComparition = sales.compareTo(thatSales);    

        if(stateComparition == 0){
            if(salesComparition > 0)
                 return -1;
            else if(salesComparition < 0)
                 return 1;
            else
                 return 0;
        }
           return stateComparition;         
    }

    public String toString(){
        String result = "";
        result = "Owner =  " + getOwner() + " ,State = "+ getLocation() + " ,Sales = " + getTotal() + "\n";

        return result;
    }

}

这个类有一个compareTo方法。我想知道你们是否可以详细解释一下这个方法在做什么......基本上我需要你们跟踪它并一步一步解释我。谢谢你们!

package prob1;
import java.util.ArrayList;
import java.util.Collections;

public class FracnahiseTester {

    /**
     * @param args
     */
    public static void main(String[] args) {
         ArrayList<Franchise> franchises = new ArrayList<Franchise>();

         Franchise franchaise1 = new Franchise("CA",3300,"Corey");
         Franchise franchaise2 = new Franchise("GA",1000,"Pato");
         Franchise franchaise4 = new Franchise("NC",3300,"Travis");
         Franchise franchaise3 = new Franchise("NC",3500,"Roy");

         franchises.add(franchaise1);
         franchises.add(franchaise2);
         franchises.add(franchaise4);
         franchises.add(franchaise3);

         Collections.sort(franchises);
         for(Franchise i : franchises){
             System.out.println(i);
         }
    }

}

这是我的测试人员

1 个答案:

答案 0 :(得分:1)

虽然您可以从javadoc获取此信息,但这个答案将有助于理解compareTo()方法的概念。

在实施Comparable界面时,您的compareTo()方法用于比较不同的对象,

public int compareTo(Franchise that) {
    int stateComparition = this.getLocation().compareTo(that.getLocation()); 
    Double sales = Double.valueOf(this.getTotal());    
    Double thatSales = Double.valueOf(that.getTotal());
    int salesComparition = sales.compareTo(thatSales);    

    if(stateComparition == 0){
        if(salesComparition > 0)
             return -1;
        else if(salesComparition < 0)
             return 1;
        else
             return 0;
    }
       return stateComparition;         
}
  • public int compareTo()用于将作为参数传递的对象与调用此方法的对象进行比较。
  • 如果this对象小于that个对象,则会返回一个负整数,在您的情况下为-1。这意味着this个对象在that之前以排序方式排在第一位。
  • 如果this等于that个对象,则0应相等,表示对象相等。
  • 如果this大于that个对象,则返回正整数,在您的情况下,它是1。这意味着thisthat类型的对象排序后Franchise出现。