Selection根据Shoe Id按升序对存储在Array中的鞋进行排序



我正在尝试使用选择排序来根据shoeId对数组中的鞋进行排序。排序按升序排列。我是拧选排序递归的方式。我面临的一个问题是在sortShoesRecurse方法中。它看起来不喜欢我在那里使用的compareToo方法,但我硬着头皮使用compareToO方法。

<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的比较方法

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,保存Shoes。

现在,假设int getShoeId()可用,在Java8中对其进行排序非常容易,它将是:

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

就这样!不过,我在这里使用的有点复杂,基本上Shoe::getShoeId()是一个方法引用,它在Shoe上被调用并返回int。然后Comparator.comparingInt(...)神奇地创建了Comparator<Shoe>,最后shoes.sort(...)简单地对列表进行排序。

在Java7中,您需要编写自定义比较器,我一直觉得这很棘手,所以我建议使用Java8,它将于2014年3月18日公开发布。

我的猜测是,在您的Show类中,您已经声明id具有Primitive int:

int id;

由于int是基元类型,您无法在其上调用函数。将成员变量"id"的类型更改为Integer,它就会起作用:

Integer id;
public Integer getId()
{
return id;
}

我建议您比较Shoe对象本身,而不是通过ID进行比较。在这种情况下,您可以将"id"定义为基元int。

public class Shoe implements Comparable<Shoe>
{
public int compaare(Shoe obj)
{
return id - obj.id;
}
}

最新更新