【问题标题】:Bubblesort Array Ascending [closed]冒泡排序数组升序[关闭]
【发布时间】:2012-11-14 14:55:51
【问题描述】:

我有一个数据文件,我需要按车辆的品牌和型号对信息进行排序。我的冒泡排序不起作用,你能帮我解决我的问题吗?非常感谢! 附言我不能有额外的方法:(如果我删除 getMake() 方法,它可以工作,但 && getModel 根本不起作用:(

    public static void sortByVehicleMakeModel(Vehicle[] vehicles) {
        for(int y = 0; y < vehicles.length; y++) {
            for (int x = 0 ; x < vehicles.length - 1 ; x++){
                if((vehicles[x].getMake().compareToIgnoreCase(vehicles[x+1].getMake()) > 0) && (vehicles[x].getModel().compareToIgnoreCase(vehicles[x+1].getModel()) > 0)) {    
                    swap(vehicles, x, x + 1);
                }           
            }
        }
        for(int x = 0; x < vehicles.length - 1; x++){
            System.out.println(vehicles[x].getMake());
        }
    }

【问题讨论】:

  • 怎么了?如果它抛出错误,你能给出一个堆栈跟踪,或者描述它给出的错误输出吗?
  • @awolfe91 没有错误,只是没有排序!!!
  • 它不起作用,因为只有在 AND 制造的模型不同时才交换它们。您需要考虑到make 相同,但model 不同的情况。看我的回答...

标签: java arrays sorting object methods


【解决方案1】:

按照以下步骤比较两辆车:

  • 比较两辆车的品牌
  • 如果它们相等,则返回比较结果
  • 如果他们相等,比较模型并返回结果

您应该将 if 语句中的代码替换为使用上述逻辑比较两个车辆的方法。

类似这样的:

if (compareVehicles(vehicles[x], vehicles[x + 1]) > 0) {
    swap(vehicles, x, x + 1);
}

要以正确的方式做到这一点,你应该让Vehicle实现Comparable

这样你就可以把上面的逻辑放在你的compareTo方法中。

这将允许您简单地执行此操作:

if (vehicles[x].compareTo(vehicles[x + 1]) > 0) {
    swap(vehicles, x, x + 1);
}

下面是一个如何实现 Comparable 的简单示例:

class Vehicle implements Comparable<Vehicle> {
    private String make;
    private String model;

    public int compareTo(Vehicle other) {
        if (other == null) {
            return -1;
        }
        int compareVal = make.compareToIgnoreCase(other.make);
        if (compareVal == 0) {
            return model.compareToIgnoreCase(other.model);
        }
        else {
            return compareVal;
        }
    }

}

好的...因为已经有几天了,我将向您展示如何操作。

public static void sortVehicles(Vehicle[] vehicles) {
    for (int i = 0; i < vehicles.length - 1; i++) {
        Vehicle curr = vehicles[i];
        Vehicle next = vehicles[i + 1];
        String currMake = curr.getMake();
        String nextMake = next.getMake();
        int compareVal = currMake.compareToIgnoreCase(nextMake);
        // if the makes are the same, we need to compare the models
        if (compareVal == 0) {
            String currModel = curr.getModel();
            String nextModel = next.getModel();
            compareVal = currModel.compareToIgnoreCase(nextModel);
        }
        if (compareVal > 0) {
            swap(vehicles, i, i + 1);
        }
    }

    for (Vehicle v : vehicles) {
        System.out.println(v.getMake());
    }
}

【讨论】:

  • 谢谢你,但我才意识到我不能有额外的方法:(我怎样才能做到一举两得?
  • @Dani - 代码是否在方法中并不重要。只需使用上面的逻辑来比较两个 Vehicles 并将比较结果存储为 int。然后您可以在 if 语句 中检查 int 是否大于零。提示:使用上面 compareTo 方法中的逻辑来设置 int 的值。
  • 我试过了,但没用 :(
  • @Dani - 随意编辑您的问题以包含您当前的代码。我很乐意看看并提供一些建议。如果您遇到错误或异常,描述它们会有所帮助。
  • public int compareTo(其他车辆) { 2 int val = make.compareToIgnoreCase(other.make); 3 if(val != 0) 4 返回值; 5 6 return model.compareToIgnoreCase(other,model); 7 }
【解决方案2】:

只是为了提高性能(我意识到比较逻辑,@jahroy 是对的)。我认为在您的情况下,第二个循环的代码应如下所示: x

    for(int y = 0; y < vehicles.length; y++) {
        for (int x = 0 ; x < vehicles.length - y -1 ; x++){
            if((vehicles[x].getMake().compareToIgnoreCase(vehicles[x+1].getMake()) > 0) && (vehicles[x].getModel().compareToIgnoreCase(vehicles[x+1].getModel()) > 0)) {    
                swap(vehicles, x, x + 1);
            }           
        }
    }

【讨论】:

  • 谢谢你,但没有改变它:(
【解决方案3】:

这是我修复它的方法:

public static void sortByVehicleMakeModel(Vehicle[] vehicles) {


    for(int y = 0; y < vehicles.length; y++) {
        for (int x = 0 ; x < vehicles.length - 1 ; x++){
            boolean compare1 = (vehicles[x].getMake().compareToIgnoreCase(vehicles[x+1].getMake()) > 0);

        if (compare1){
            swap(vehicles, x, x + 1);

        }       

    }           
}

【讨论】:

  • 这不适用于相同品牌和不同型号的汽车。
  • 不需要有两个循环。你永远不会使用变量y。如果您有 20 辆车,您当前的代码将无缘无故地运行 19 次。它会在y = 0 时完成所有工作,然后它会再运行 19 次无所事事(但浪费时间和精力)。
  • 好吧,伙计们冷静下来,这一切都很好,没有问题 :) 感谢您的关注
  • 这段代码无法正常工作。
猜你喜欢
  • 1970-01-01
  • 2015-03-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 2013-09-28
  • 2013-06-28
相关资源
最近更新 更多