【问题标题】:Method to sort an array from least to greatest Java [closed]从最小到最大Java对数组进行排序的方法[关闭]
【发布时间】:2014-10-06 17:10:39
【问题描述】:

您好,我正在尝试创建一个查找数组模式的方法。为此,我首先需要创建一个将列表从最小到最大排序的方法,但我不确定我首先做错了什么

public class Mode {
    public int [] sort(int[] asd) {
        int[] sorted = new int[10];
        for (int i = 0; i < asd.length; i++) {
            for (int j = 1; j < asd.length; j++) {
                if ( (asd[i] > asd[j]) && (i != j) ) {
                    int temp = asd[j];
                    asd[j] = asd[i];
                    asd[i] = asd[temp];
                }
                else
                    continue;
            }
        }
        return sorted;
    }

    public static void main(String[] args) {
        Mode list1 = new Mode();
        int[] array = {3,2,5,4,1,1,1,1,10,9};
        int[] potato = list1.sort(array);
        for (int i = 0; i < potato.length; i++)
            System.out.print(potato[i]);
    }
}

当我运行它时,我得到 0000000000 作为输出。我认为该方法有问题,因为我返回了一个已初始化但没有放入任何内容的数组。我如何(在我的嵌套 for 循环中)添加排序数组中的每个数字?

【问题讨论】:

  • asd[i] = asd[temp]; 应该是asd[i] = temp;
  • 您正在就地排序数组asd,但随后返回初始化为零的sorted。尝试将数组asd 复制到sorted,然后对数组sorted 进行排序。
  • 哦,好吧,我做到了,但仍然没有做得很好。我还将 return 更改为 return asd 而不是 sorted
  • @vikingsteve 不确定你的意思

标签: java sorting


【解决方案1】:

您的代码中存在一些问题,它们已按以下方式修复:

public class Mode {
    public int [] sort(int[] asd) {
        int[] sorted = asd.clone();
        for (int i = 0; i < sorted.length; i++) {
            for (int j = i+1; j < sorted.length; j++) {
                if ( (sorted[i] > sorted[j]) && (i != j) ) {
                    int temp = sorted[j];
                    sorted[j] = sorted[i];
                    sorted[i] = temp;
                }
            }
        }
        return sorted;
    }

    public int findMode(int[] sorted) {
        // do whatever you want to do here...
        return 0;
    }

    public static void main(String[] args) {
        Mode list1 = new Mode();
        int[] array = {3,2,5,4,1,1,1,1,10,9};
        int[] potato = list1.sort(array);
        for (int i = 0; i < potato.length; i++) {
            System.out.print(potato[i] + ",");
        }
        System.out.println();
        System.out.print(list1.findMode(potato));
    }
}

解释一下,之前您对asd 进行排序。现在我们正在对sorted 进行排序,它以asd 的副本开头。

j 的循环应该从 i+1 开始。

“交换”操作的最后一部分应该是sorted[i] = temp

【讨论】:

  • 谢谢!我还有一个问题。那么既然这是在一个方法中,我可以在另一个方法中调用排序列表吗?就像我有另一种方法 public int[] findMode(sorted)
  • 是的,你当然可以。 sorted 数组在堆内存中正确分配(通过clone()),它会在内存中持久存在,并且确实可以以任何您希望的方式在其他方法中使用,只要您保持对它的引用(之后,它将最终被垃圾收集)。
  • 我尝试这样做,但出现错误,提示需要标识符。这就是我所做的 public int findMode(sorted) 还是应该只做 public int findMode() 并将 sorted 放入方法本身?
  • 那么当你调用findMode 方法时,你应该传入正确的变量。这在您的主要方法的末尾将是findMode(potato)。该方法的正确声明是public int findMode(int[] sorted) { ... }。请查看我修改后的答案 - 这有帮助吗?
  • public 和 private 是 java 中四个 access modifiers 中的两个,它们确定方法是否可以被其他类访问(调用) - google java access modifiersencapsulation 查找一些不错的概述。玩得开心!
【解决方案2】:

您可以使用java.util.Arrays对数组进行升序排序

public class SortArray {
    public static void main(String[] args) {
        int[] array = { 3, 2, 5, 4, 1, 1, 1, 1, 10, 9 };
        Arrays.sort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
}

上述代码 sn-p 的输出将是

1 1 1 1 2 3 4 5 9 10 

【讨论】:

    【解决方案3】:

    您在排序方法中分配给asd 的元素,然后返回数组sorted,它在您的sort 方法中永远不会改变。

    此外,您应该将sorted 数组初始化为与asd 相同的大小。

    【讨论】:

    • 我摆脱了已排序的数组,只返回 asd 但无法正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2021-08-12
    • 2019-05-11
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多