2014-02-28 39 views
1

我正嘗試使用選擇排序來根據shoeId對鞋子進行排序。排序按升序排列。我正在選擇遞歸的方式。我面臨的一個問題是sortShoesRecurse方法。 IT看起來不像我在那裏使用的compareTo方法,但我使用了compareTo方法。選擇根據鞋號升序存儲在陣列中的鞋子的排序

<pre> <code> 
if(sc.compare(sh[indexWithMinValue], sh[forwardIndex]) > 0) 
</pre> </code> 

當我運行程序我收到此錯誤:

<pre> <code> 

java.lang.NullPointerException 
at shoepkg.ShoeComparator.compare(ShoeComparator.java:8) 
    //line 8 in the code 
    // if (obj1.getId() == obj2.getId()) 

    at shoepkg.ShoeProcessor.sortShoesRecurse(ShoeProcessor.java:43) 
    //if(sc.compare(sh[indexWithMinValue], sh[forwardIndex]) > 0) 

at shoetestpkg.TestShoe.main(Shoe.java:28) 
    //bp.sortShoesRecurse(0); 

    </pre> </code> 




<pre> <code> 

    public class TestShoe { 


    public static void main(String[] args) { 

    ShoeProcessor s = new Shoe(); 
    Shoe s1 = new Shoe(7, "Black"); 
    Shoe s2 = new Shoe(10, "Red"); 

    try { 
     s.addShoe(s1); 
        s.addShoe(s2); 

    }catch(ShoeException bex){ 
     System.out.println("Shoe Exception: " + bex); 
    } 


    } 
    } 

    public class ShoeProcessor 
    { 
    private Shoe [] sh; 
    private int numShoes=0; 
    private ShoeComparator<Shoe> sc; 

    public ShoeProcessor() 
    { 
    sh = new Shoe [10]; 
    sc=new ShoeComparator<Shoe>(); 
    } 


    public void addShoe(Shoe s) throws ShoeException 
    { 
    if(s.getId() < 0) { 
     throw new ShoeException(s); 
    } 
    else { 
     if(numShoes<10){ 
      sh[numShoes]=s; 
      numShoes++; 
     } 
    } 
    } 

    public void sortShoesRecurse(int startIndex) 
    { 
     if (startIndex >= sh.length - 1) { 
      return; 
     } 

     int indexWithMinValue=startIndex; 


     for(int forwardIndex=startIndex+1; forwardIndex<sh.length;forwardIndex++) { 
      if(sc.compare(sh[indexWithMinValue], sh[forwardIndex]) > 0) { 
       indexWithMinValue = forwardIndex; 
      } 
     } 
     Shoe temp= sh[startIndex]; 
     sh[startIndex]=sh[indexWithMinValue]; 
     sh[indexWithMinValue]= temp; 

     sortShoesRecurse(startIndex+1); 
    } 

    public Book[] getBooks() { 
     return books; 
    } 
    } 

    package shoepkg; 

    public class ShoeComparator<T extends Shoe> 
    { 

    public int compare(T obj1, T obj2) 
    { 
      if (obj1.getId()== obj2.getId()) 
      { 
       return 0; 
      } 
      if (obj1.getId() > obj2.getId()) 
      { 
      return 1; 
      } 
      else if (obj1.getId() < obj2.getId()) 
      { 
       return -1; 
      } 
      return 0; 
    } 
    } 

</pre> </code> 

我做了一些更新,提出了一些建議後的代碼,這是當前的代碼。仍然得到一些錯誤,這些錯誤也在頂部進行了更新。感謝您的幫助。我不得不根據Id來比較對象。

回答

0

我的猜測是,在你展示類,你都宣稱ID具有原始INT:

int id; 

由於int是你不能在它調用函數基本類型。更改成員變量「ID」爲整數的類型,它會工作:

Integer id; 

    public Integer getId() 
    { 
    return id; 
    } 

我建議你比較擦鞋對象本身,而不是通過暴露的ID進行比較。在這種情況下,您可以將'id'定義爲基本int。

public class Shoe implements Comparable<Shoe> 
    { 

     public int compaare(Shoe obj) 
     { 
     return id - obj.id; 
     } 
    } 
+0

我更新了我的主帖。我需要使用兩個對象進行比較,並需要根據它們的ID進行比較。 – user1282256

2

首先,讓我們打破的是拋出你的錯誤代碼行:

sh[indexWithMinValue].getId().sc.compareTo(sh[forwardIndex].getId()) > 0 

所以:

sh[indexWithMinValue].getId() 

從你的鞋陣列和呼叫得到一個擦鞋對象getId()方法。

.sc 

詢問無論getId()返回它的'sc'屬性。

compareTo(sh[forwardIndex].getId()) > 0 

並將'sc'屬性與數組中另一個Shoe對象的'Id'進行比較。

你可能會開始看到現在你的問題:)(提示:在INT從的getId()返回不具有「SC」屬性)

其次,讓我們來看看你的ShoeComparator的比較方法

public int compare(T obj1, T obj2) 

它需要對象,而不是一個!

有2種方式輕鬆解決這個矛盾:

1:正確地對您的ShoeComparator的比較()實現呼叫:

if (sc.compare(sh[indexWithMinValue, sh[forwardIndex]) > 0) 

這樣你正確使用你的比較(),並且它'應該'不再拋出錯誤!這是因爲你的compare()方法在內部調用getId()並對它進行比較,所以在調用此方法之前不必執行此操作。

2:放下你的整個ShoeComparator類,並讓你的鞋子類實現Comparable接口,這看起來是這樣的:

public class Shoe implements Comparable<Shoe> { 

    private int id; 

    // rest of your Shoe class 

    @Override 
    public int compareTo(Shoe shoe) { 
     if (id == shoe.getId()) 
      return 0; // Shoes are the same! 

     if (id > shoe.getId()) 
      return 1; // I'm bigger than the other Shoe! 

     if (id < shoe.getId()) 
      return -1; // I'm smaller :((

     return 0; 
    } 

} 

然後,您可以修改你的if語句看起來像這樣:

if (sh[indexWithMinValue].compareTo(sh[forwardIndex]) > 0) { 
+0

威廉。謝謝你的解釋。我申請了所有改變,你建議我到代碼並更新我的帖子。仍然有一些錯誤,但現在它對我來說更加敏感。非常感謝。 – user1282256

2

你似乎是以非常麻煩的方式做到這一點,雖然這可能不會回答你的直接問題,它將有望在未來幫助你:

你應保持List<Shoe> shoes某處,持有Shoe s。

現在排序它在Java中8,假設int getShoeId()是可用的,是很容易的,那就是:

shoes.sort(Comparator.comparingInt(Shoe::getShoeId)); 

就是這樣!我在這裏使用的是更復雜的,但基本上Shoe::getShoeId()是一個方法的參考,這是在Shoe上調用,並返回int。然後Comparator.comparingInt(...)做到了創造Comparator<Shoe>的魔力,然後最後shoes.sort(...)只是對列表進行排序。

在Java 7,你需要編寫自定義的比較,這是我一直都認爲是棘手的,所以我建議使用Java 8,這將是供公衆發佈3月18日2014年

+0

感謝您的支持。我知道比較器,但我需要在這種情況下寫我自己的比較方法,並比較Shoe對象的ID。 – user1282256

+0

@ user1282256我不明白你爲什麼要重新實現公共庫已經實現的方法。 – skiwi

+0

這是常見的使用比較(T obj1,T obj2) – user1282256