Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据鞋子Id按升序对存储在数组中的鞋子进行选择排序_Java_Arrays_Sorting - Fatal编程技术网

Java 根据鞋子Id按升序对存储在数组中的鞋子进行选择排序

Java 根据鞋子Id按升序对存储在数组中的鞋子进行选择排序,java,arrays,sorting,Java,Arrays,Sorting,我正在尝试使用选择排序来根据shoeId对数组中的鞋进行排序。排序是按升序进行的。我正在以递归的方式编写选择排序。我面临的一个问题是SORTSHOESREARCH方法。看起来它不像我在那里使用的compareTo方法,但我选择了compareTo方法 <pre> <code> if(sc.compare(sh[indexWithMinValue], sh[forwardIndex]) > 0) </pre> </code> int id

我正在尝试使用选择排序来根据shoeId对数组中的鞋进行排序。排序是按升序进行的。我正在以递归的方式编写选择排序。我面临的一个问题是SORTSHOESREARCH方法。看起来它不像我在那里使用的compareTo方法,但我选择了compareTo方法

<pre> <code>
if(sc.compare(sh[indexWithMinValue], sh[forwardIndex]) > 0)
</pre> </code>
  int id;
当我运行程序时,我收到以下错误:

我做了一些更新的代码后,一些建议,这是目前的代码。仍然会出现一些错误,这些错误也会在顶部更新。谢谢你的帮助


我必须根据Id比较对象。

我的猜测是,在Show类中,您已声明Id具有基元int:

sh[indexWithMinValue].getId().sc.compareTo(sh[forwardIndex].getId()) > 0
因为int是基元类型,所以不能对其调用函数。将成员变量“id”的类型更改为整数,它将起作用:

sh[indexWithMinValue].getId()
我建议您比较鞋子对象本身,而不是通过ID公开比较。在这种情况下,可以将“id”定义为基元int

.sc

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

sh[indexWithMinValue].getId().sc.compareTo(sh[forwardIndex].getId()) > 0
因为int是基元类型,所以不能对其调用函数。将成员变量“id”的类型更改为整数,它将起作用:

sh[indexWithMinValue].getId()
我建议您比较鞋子对象本身,而不是通过ID公开比较。在这种情况下,可以将“id”定义为基元int

.sc

首先,让我们分解引发错误的代码行:

compareTo(sh[forwardIndex].getId()) > 0
因此:

从Shoe数组中获取Shoe对象并调用getId方法

if (sc.compare(sh[indexWithMinValue, sh[forwardIndex]) > 0)
询问为其“sc”属性返回的getId

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;
    }

}
并将“sc”属性与数组中另一个Shoe对象的“Id”进行比较

您现在可能开始看到您的问题:提示:从getId返回的int没有“sc”属性

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 (sh[indexWithMinValue].compareTo(sh[forwardIndex]) > 0) {
它需要2个对象,而不是一个

有两种方法可以轻松解决此冲突:

1:正确地调用您的ShoeComparator的比较实现:

shoes.sort(Comparator.comparingInt(Shoe::getShoeId));
通过这种方式,您可以正确地使用比较实现,它“应该”不再抛出错误!这是可行的,因为compare方法在内部调用getId并对其进行比较,所以在调用此方法之前不必这样做

2:删除您的整个ShoeComparator类,让您的Shoe类实现可比较的接口,如下所示:


首先,让我们分解引发错误的代码行:

compareTo(sh[forwardIndex].getId()) > 0
因此:

从Shoe数组中获取Shoe对象并调用getId方法

if (sc.compare(sh[indexWithMinValue, sh[forwardIndex]) > 0)
询问为其“sc”属性返回的getId

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;
    }

}
并将“sc”属性与数组中另一个Shoe对象的“Id”进行比较

您现在可能开始看到您的问题:提示:从getId返回的int没有“sc”属性

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 (sh[indexWithMinValue].compareTo(sh[forwardIndex]) > 0) {
它需要2个对象,而不是一个

有两种方法可以轻松解决此冲突:

1:正确地调用您的ShoeComparator的比较实现:

shoes.sort(Comparator.comparingInt(Shoe::getShoeId));
通过这种方式,您可以正确地使用比较实现,它“应该”不再抛出错误!这是可行的,因为compare方法在内部调用getId并对其进行比较,所以在调用此方法之前不必这样做

2:删除您的整个ShoeComparator类,让您的Shoe类实现可比较的接口,如下所示:


您似乎在以一种非常麻烦的方式进行此操作,虽然这可能无法回答您的直接问题,但它有望在将来帮助您:

你应该把鞋子放在某处,拿着鞋子

现在,假设int-getShoeId可用,用Java 8对其进行排序非常容易,它将是:

就这样!我在这里使用的是更复杂的方法,基本上Shoe::getShoeId是一个方法引用,它在Shoe上被调用并返回一个int。然后是Comparator.comparingit。。。做魔术来创建比较器,最后是鞋子。排序。。。只需对列表进行排序


在Java 7中,您需要编写自定义比较器,我一直认为这很棘手,因此我建议使用Java 8,该比较器将于2014年3月18日公开发布。

您似乎以一种非常麻烦的方式完成此操作,虽然这可能无法回答您的直接问题,但它有望在未来帮助您:

你应该把鞋子放在某处,拿着鞋子

现在,假设int-getShoeId可用,用Java 8对其进行排序非常容易,它将是:

就这样!我在这里使用的是更复杂的方法,基本上Shoe::getShoeId是一个方法引用,它在Shoe上被调用并返回一个int。然后是Comparator.comparingit。。。做魔术来创建比较器,最后是鞋子。排序。。。只需对列表进行排序


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

我更新了我的主要帖子。我需要对两个对象进行比较,并根据它们的ID对它们进行比较。我更新了我的主要帖子。我需要使用compare with两个对象,并需要根据它们的ID对它们进行比较。谢谢你的解释。我将你建议我的所有更改应用到代码中,并更新了我的帖子。仍然会有一些错误,但现在这对我来说意义更大了。非常感谢你,威廉。谢谢你的解释。我将你建议我的所有更改应用到代码中,并更新了我的帖子。仍然会有一些错误,但现在这对我来说意义更大了。非常感谢。谢谢你的告别。我确实知道Comparator,但在本例中,我需要编写我自己的compare方法和Shoe对象的compare ID。@user1282256我不明白您为什么要重新实现已由公共库实现的方法。通常使用compareT obj1,Tobj2@user1282256我没有看到您在代码中的任何地方扩展Comparator;在鞋店,谢谢你的告别。我确实知道Comparator,但在本例中,我需要编写我自己的compare方法和Shoe对象的compare ID。@user1282256我不明白您为什么要重新实现已由公共库实现的方法。通常使用compareT obj1,Tobj2@user1282256我没有看到您在代码中的任何地方扩展Comparator;在鞋机里。