使用冒泡排序和可比较的数组列表对矩形区域进行排序

我正在构建之前的程序,该程序使用排序算法对数组中的整数进行排序。

我现在的任务是创建一个矩形的 ArrayList,创建一个扩展 Comparable 接口的 Rectangle 类,并在我的排序方法中实现泛型。

最后,程序需要对给定的 10 个不同矩形的面积值进行计算和排序。

我遇到的问题是,我不确定如何修改排序算法来实现泛型,并使用我在 Rectangle 类中创建的compareTo/方法。getArea

我对所有这些概念都相当陌生,所以我的理解是有限的。

以下是我如何创建 10 个矩形的 ArrayList 的片段(按降序值排列):

public static void main(String[] args) {


    ArrayList<Rectangle> list = new ArrayList<>();


    for (int i=10;i>=1;i--) {

        Rectangle rectangle = new Rectangle((i+5)*2,(i+7)*6);

        list.add(rectangle);

    }

    ...

}

这是我的 Rectangle 类,它扩展了 Comparable 接口并具有重写的 CompareTo 方法:


class Rectangle implements Comparable<Rectangle> {


    double width=1;

    double height=1;


    public Rectangle() { }


    public Rectangle(double recWidth, double recHeight) {

        this.width = recWidth;

        this.height = recHeight;

    }


    public double getWidth() {

        return width;

    }


    public void setWidth(double width) {

        this.width = width;

    }


    public double getHeight() {

        return height;

    }


    public void setHeight(double height) {

        this.height = height;

    }


    public double getArea() {

        return width * height;

    }


    @Override

    public int compareTo(Rectangle r) {


        if (getArea() > r.getArea()) {

            return 1;

        }

        else if (getArea()<r.getArea()) {

            return -1;

        }

        else {

            return 0;

        }   

    }

    ...

}

这是我目前的冒泡排序方法,该方法不再起作用。


public static <R extends Comparable <Rectangle>> void bubbleSort(ArrayList<Rectangle> list) {


    for (int k = 1; k < list.size();k++) {

        for (int i = 0; i <list.size()-k; i++) {   


             R elem = list.getArea();

             R elem2 = list.getArea(i+1);


             if (elem.compareTo(elem2) >= 1) {

                 R temp = list.get(i);    

                 list.set(i, list.get(i+1)); 

                 list.set(i+1, temp); 

             }

         }

         printList(list);


    }


HUH函数
浏览 27回答 1
1回答

MMMHUHU

getArea()您正在尝试调用对象的方法ArrayList<>。然而,该类ArrayList<>没有这样的方法。无法拨打电话R&nbsp;elem&nbsp;=&nbsp;list.getArea();当list是 类型时ArrayList<>。实际上,您根本不需要调用getArea()冒泡排序方法。冒泡排序方法只关心列表中是否包含实现该Comparable接口的对象。您的方法声明应更改为:public&nbsp;static&nbsp;<R&nbsp;extends&nbsp;Comparable&nbsp;<R>>&nbsp;void&nbsp;bubbleSort(ArrayList<R>&nbsp;list)这样你就可以说:“列表必须包含通用类型的对象R,其中类R实现了Comparable<R>接口”。当您在脑海中替换R为您的Rectangle班级时,您会发现您的班级Rectangle implements Comparable<Rectangle>符合该要求。在您的冒泡排序方法中,您不再使用该getArea()方法(您不能),而只能使用来自 的方法以及接口提供的ArrayList<>方法。您的代码应如下所示:compareTo()Comparable<>public static <R extends Comparable <R>> void bubbleSort(ArrayList<R> list) {&nbsp; &nbsp; for (int k = 1; k < list.size();k++) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i <list.size()-k; i++) {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R elem = list.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R elem2 = list.get(i+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (elem.compareTo(elem2) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.set(i, elem2);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.set(i+1, elem);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printList(list);&nbsp; &nbsp; }}您正在使用get(i)和get(i+1)来获取您想要检查的对。然后在if()语句中您只需交换位置即可。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java