2017-12-18 44 views
0

这里是我的接口类的Java Collections.sort(),即使媲美声明

public interface Thing { 
    int getVolume(); 
} 

这里没有工作是实现东西

Item.java

public class Item implements Thing, Comparable<Thing> { 
    private String name; 
    private int volume; 

    public Item(String name,int volume){ 
     this.name = name; 
     this.volume = volume; 
    } 

    @Override 
    public int getVolume() { 
     return this.volume; 
    } 

    public String getName(){ 
     return this.name; 
    } 

    @Override 
    public String toString(){ 
     return name+" ("+volume+" dm^3)"; 
    } 

// @Override 
    @Override 
    public int compareTo(Thing another) { 
     if(this.getVolume() < another.getVolume()){ 
      return -1; 
     } 

     if(this.getVolume() == another.getVolume()){ 
      return 0; 
     } 
     else{ 
      return 1; 
     } 
    } 

} 

类当我尝试使用以下命令运行主程序时,它运行正常 // main program.java

public class Main { 

    public static void main(String[] args) { 
     // test your program here 
    List<Item> items = new ArrayList<Item>(); 
    items.add(new Item("passport", 2)); 
    items.add(new Item("toothbrash", 1)); 
    items.add(new Item("circular saw", 100)); 

    Collections.sort(items); 
    System.out.println(items); 



    } 
} 

但是,当我尝试在它实现Thing接口,另一个类运行Collections.sort(),我得到一个错误

这里框类,它实现的事情接口,当我尝试运行Collections.sort(store)在void sort()函数中给出了一个错误,即使商店是List并且Box类实现了Thing接口,并且我已经在Item.java类中为Thing定义了类似的东西

Box.java

public class Box implements Thing { 

    private int maximumCapacity; 
    private List<Thing> store; 

    public Box(int maximumCapacity) { 
     this.maximumCapacity = maximumCapacity; 
     this.store = new ArrayList<Thing>(); 
    } 

    public boolean addThing(Thing thing) { 
     // I.E. if the item added does not make the total volume go to max capacity only 
     // then add 
     if (this.getVolume() + thing.getVolume() < this.maximumCapacity) { 
      store.add(thing); 
      return true; 
     } 
     return false; 
    } 

    @Override 
    public int getVolume() { 
     // we calculate things of all items in the boxes (current value) 
     int currentWeight = 0; 
     for (Thing t : store) { 
      currentWeight += t.getVolume(); 
     } 
     return currentWeight; 
    } 

    public List<Thing> getStore() { 
     return store; 
    } 

    public int numOfItems(){ 
     return this.store.size(); 
    } 


    public void sort(){ 

     Collections.sort(store); // *****does not work ****// 

    } 

} 

它给出了上面的错误排序为“没有合适的方法找到 排序(列表<东西>)”。

我的问题是,如果它可以在main.java程序中的项目被列为List,那么为什么它不能在这里工作? 如何解决它?

+2

的建议你'Box'类未实现'Comparable'。你如何期待'sort'知道如何排序? – njzk2

回答

3

它的主类,你排序List<Item>哪里Item implements Thing, Comparable<Thing>

Box类中,您尝试对List<Thing>进行排序,但Thing本身不执行Comparable<Thing>。因此Java不知道如何对Thing进行排序。

要解决它,你要么必须两个Thing小号提供比较(如提出АлександрНестеров)或声明Thing implements Comparable<Thing>

public interface Thing extends Comparable<Thing>{ 

    int getVolume(); 

    //provide default method to sort any class which implements Thing 
    @Override 
    public default int compareTo(Thing another) { 
     return Integer.compare(this.getVolume(), another.getVolume()); 
    } 
} 
+0

非常感谢您通过制作单独的Comparator公共类Sort实现比较器 { @覆盖 公众诠释比较(事O1,O2的东西){ 如果(o1.getVolume()” –

2

这是因为首先你一种“商品”,在第二个您排序“名单的事情”
所以,你可以通过使用lambda修复:

Collections.sort(store, (o1, o2) -> { 
your implementation of comparator 
}); 
+0

如果您使用的是jdk 8或更高版本,并且希望所有实现“Thing”的类都应该在相同参数的基础上进行排序,您应该将界面更改为: –

0

我建议你定义的东西延长可比,给您的应用程序没有按当你添加的类不是Comparable时不起作用。

顺便说一句,你的compareTo看起来相当复杂。做到这一点,而不是:

int compareTo(Thing another) { 
    return this.getVolume() - another.getVolume(); 
    } 
+0

'compareTo'方法正确实现。为什么你的实现是错误的看看https://stackoverflow.com/q/2728793/5646962 –

0

在第一个节目,你有

public class Item implements Thing, Comparable<Thing> 

但在第二,你就必须

public class Box implements Thing 

如果你想排序工作,你需要要么实现Comparable或Comparator(单独的类只实现Comparator)。

-1

使Thing成为实现Comparable的抽象类,以便Thing随时准备好进行排序。项目可以从东西

0

如果您使用的是JDK 8或以上扩展,并希望实现“物”的所有类应在同一参数的基地进行排序,你应该你的界面更改此提供默认的排序能力:

//extend your interface with comparable 

public interface Thing extends Comparable<Thing>{ 

int getVolume(); 

//provide default method to sort any class which implements Thing 
@Override 
public default int compareTo(Thing another) { 
    if(this.getVolume() < another.getVolume()){ 
     return -1; 
    } 

    if(this.getVolume() == another.getVolume()){ 
     return 0; 
    } 
    else{ 
     return 1; 
    } 
} 

} 

现在Item和Box只需要实现Thing接口。

请尽量也优化的compareTo()方法,通过 @Jo威特斯

+0

它不工作给我错误时,我这样做。 –