[英]Selection Sort of the shoes stored in the Array based on the Shoe Id in ascending order
我正在嘗試使用選擇排序對基於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比較對象。
首先,讓我們分解引發錯誤的代碼行:
sh[indexWithMinValue].getId().sc.compareTo(sh[forwardIndex].getId()) > 0
所以:
sh[indexWithMinValue].getId()
從Shoe數組中獲取Shoe對象,然后調用getId()方法。
.sc
詢問為其“ sc”屬性返回的任何getId()。
compareTo(sh[forwardIndex].getId()) > 0
並將'sc'屬性與數組中另一個Shoe對象的'Id'比較。
您可能現在開始發現問題了:)(提示:從getId()返回的int沒有'sc'屬性。)
其次,讓我們看一下ShoeComparator的compare方法
public int compare(T obj1, T obj2)
它需要2個對象,而不是一個!
有兩種方法可以輕松解決此沖突:
1:正確調用ShoeComparator的compare()實現:
if (sc.compare(sh[indexWithMinValue, sh[forwardIndex]) > 0)
這樣,您可以正確使用compare()實現,並且“不再”應該拋出錯誤! 由於您的compare()方法在內部對getId()進行了調用並將其進行比較,因此您可以在調用此方法之前不必執行此操作。
2:刪除整個ShoeComparator類,並使Shoe類實現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) {
您似乎以一種非常麻煩的方式來執行此操作,盡管這可能無法回答您的直接問題,但希望它將在將來對您有所幫助:
您應該在某處維護List<Shoe> shoes
,並握住Shoe
。
現在,使用int getShoeId()
可用,在Java 8中對其進行排序非常簡單,它將是:
shoes.sort(Comparator.comparingInt(Shoe::getShoeId));
就是這樣! 不過,我在這里使用的內容稍微復雜一些,基本上Shoe::getShoeId()
是方法引用,在Shoe
上被調用並返回int
。 然后Comparator.comparingInt(...)
神奇地創建了Comparator<Shoe>
,最后, shoes.sort(...)
只是對列表進行了排序。
在Java 7中,您需要編寫自定義比較器,而我一直發現這很棘手,因此我建議您使用Java 8,它將在2014年3月18日公開發布。
我的猜測是,在您的Show類中,您聲明的id具有Primitive int:
int id;
由於int是原始類型,因此您不能在其上調用函數。 將成員變量'id'的類型更改為Integer,它將起作用:
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.