2017-12-18 47 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

它不工作給我錯誤時,我這樣做。 –