2012-11-24 147 views
1

我的程序实现了Product类,其对象包含以下实例变量:name,prioritypriceamount决定使用类比或比较器

我有我需要的LinkedList做任何其他操作之前排序ProductLinkedList对象。

我想先列出优先顺序(从最低到最高)。如果优先级相同,则查看价格(从最低到最高),然后查看名称(字母顺序)。

我已经做了大量的关于Collections.sort,ComparableComparator的阅读。我相信我需要使用Comparable接口并实施compareTo方法。我的想法是,因为prioritypricename都具有“自然”排序,所以使用Comparable更有意义。

public class Product extends ProductBase implements PrintInterface, Comparable<Product>{ 
    private String name; 
    private int priority; 
    private int cents; 
    private int quantity; 

    // setters and getters 

    /** 
    * Compare current Product object with compareToThis 
    * return 0 if priority, price and name are the same for both 
    * return -1 if current Product is less than compareToThis 
    * return 1 if current Product is greater than compareToThis 
    */ 

    @override 
    public int compareTo(Product compareToThis) 
} 

然后,当我想我的排序LinkedList的我就叫Collections.sort(LinkedList)。在我开始编写代码之前,你能告诉我我是否错过或忘记了任何东西吗?

** * ** * ** * ****UPDATE* ** * ** * ** * * * * ** * ** * ** * ** * ** * ** *

我刚刚创建了一个名为ProductComparator用比较方法单独的类。

这是LinkedList类的一部分。这

import java.util.Collections; 

public class LinkedList { 

private ListNode head; 

public LinkedList() { 
    head = null; 
} 
    // this method will sort the LinkedList using a ProductComparator 
public void sortList() { 
    ListNode position = head; 
    if (position != null) { 
     Collections.sort(this, new ProductComparator()); 
    } 
} 
// ListNode inner class 
private class ListNode { 

    private Product item; 
    private ListNode link; 

    // constructor 
    public ListNode(Product newItem, ListNode newLink) { 
     item= newItem; 
     link = newLink; 
    } 
} 

}

我从IDE收到以下错误,当我编译。

类型集合中的方法sort(List,Comparator)不适用于参数(LinkedList,ProductComparator)。

有没有人知道我为什么得到这个错误,并可以指出我在正确的方向来解决它?

+0

更新了您的问题:您是否已在ProductComparator中正确实施了Comparator? –

回答

2

您在此处定义你的产品的顺序是非常具体和

  • 可能会在你的程序的未来版本
  • 可能与上下文参数富集改变
  • 将不包括新功能

所以很难被说“自然”。

我建议定义常量,例如

public static Comparator<Product> STANDARD_COMPARATOR = new Comparator<Product>() { 
    public int compare(Product p1, Product p1) { 
     return ... 
    } 
}; 

,那么你就能够轻松地排序任何地方

Collections.sort(myProductList, Product.STANDARD_COMPARATOR); 

您的代码会以更好的方式演变为你会添加其他比较器。

就像你通常应该更喜欢继承组成,你应该尽量避免在确定不变的方式你的对象的行为。

+0

你好,我不太明白。你能提供一个更详细的建议例子吗? – user1834529

+0

@ user1834529我编辑过。它更清楚吗? –

+0

是的,我认为是这样,但我相信我会在开始编码时遇到一些问题。 – user1834529

3

如果有“自然”排序,请使用“比较”。确定订单是否“自然”的经验法则是,对象的订单总是始终是

话虽如此,是否使用Comparable或Camparator的决定不是您需要考虑太多的决定。大多数IDE都有重构工具,可以轻松实现Comparable和Comparator之间的转换。所以,如果你现在选择走错路,改变它并不需要太多的努力。

+0

当您在团队中或与客户一起编码时,您最好有良好的演变API,而不要指望您的IDE稍后再修复它们。 –

+0

完全同意。但* Comparable *接口几乎不是(使用的)公共API的一部分,除非它真的是一个自然顺序。如果有人使用它,尽管它不代表自然排序,但可能是因为他们不关心排序是什么。 – onon15

0

如果您的订单仅基于数字,Comparable会没事的。

然而,由于您的订单(有时)涉及文本的词汇顺序, 一个Comparator类是较好的,因为使用的Comparable将意味着使用 String.compareTo这将阻止你有国际化。

实现Comparator的单独类可以使用 本地化的Collator来比较字符串。例如:

public class ProductComparator 
implements Comparator<Product> { 
    private final Collator collator; 

    public ProductComparator() { 
     this(Locale.getDefault()); 
    } 

    public ProductComparator(Locale locale) { 
     this.collator = Collator.getInstance(locale); 
    } 

    public int compare(Product product1, 
         Product product2) { 

     int c = product1.getPriority() - product2.getPriority(); 
     if (c == 0) { 
      c = product1.getPrice() - product2.getPrice(); 
     } 
     if (c == 0) { 
      c = collator.compare(product1.getName(), product2.getName()); 
     } 
     return c; 
    } 
} 

无论你去与相当或比较,这是明智 确保Productequals方法,检查同一 属性作为比较的代码。

+0

谢谢VGR。那么调用Collections.sort的正确方法是什么?它是Collections.sort(MyLinkedListOfProductObjects,新的ProductComparator)吗? – user1834529

+0

我试过你的建议来实现Comparator类对象并将它传递给Collections.sort。我收到一条错误消息,我不知道为什么。我从IDE获取的错误消息是: 类型集合中的方法排序(列表,比较器)不适用于参数(LinkedList,ProductComparator)。你知道我收到这个错误,你能指出我的方向吗? – user1834529

+0

这是我的LinkedList代码的一部分。 import java.util.Collections;公共类LinkedList { } \t private ListNode head; \t public LinkedList(){ \t \t head = null; \t} \t公共无效sortlist中(){ \t ListNode位置=头; \t \t if(position!= null){ \t Collections.sort(this,new ProductComparator()); \t \t} \t} \t私有类ListNode { \t \t私人产品项目; \t \t \t \t private ListNode link; \t \t \t 公共\t ListNode(产品的newitem,ListNode NEWLINK){ \t \t \t项=的newitem; \t \t \t link = newLink; \t \t} \t} } – user1834529