gpt4 book ai didi

java - Selection 根据 Shoe Id 升序排列存储在 Array 中的鞋子

转载 作者:行者123 更新时间:2023-11-29 03:23:50 24 4
gpt4 key购买 nike

我正在尝试使用选择排序根据 shoeId 对数组中的鞋子进行排序。排序是升序的。我正在使用选择排序递归方式。我面临的一个问题是 sortShoesRecurse 方法。看起来它不喜欢我在那里使用的 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

询问 getId() 为它的“sc”属性返回的任何内容。

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 接口(interface),如下所示:

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) {

关于java - Selection 根据 Shoe Id 升序排列存储在 Array 中的鞋子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22107237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com