JAVA:按降序排列数组

问题描述:

我想按降序对数组进行排序,我知道有很多在线排序数组的例子,但我只是想尝试以我自己的方式来做(只是尝试以测试算法是否可以实际工作)。但由于某些原因,我无法输出结果存储数组,我尝试使用System.out.println(Arrays.toString(myList));并且一次打印它们,它适用于我创建的一个数组,但是当尝试通过循环修改数组时,它拒绝输出任何内容,没有错误,没有任何内容,就好像没有任何内容。您的帮助将不胜感激。看下面的代码。谢谢。JAVA:按降序排列数组

import java.util.Arrays; 

public class TestArray { 

    public static void main(String[] args) { 
     double[] myList = {1.9, 2.9, 9.2, 3.4, 4.2, 6.7, 3.5}; 
     double[] sortedList = new double[7] ; 


     // Print all the array elements 
     for (double i: myList) { 
     System.out.println(i + " "); 
     } 

     // Summing all elements 
     double total = 0; 
     for (double x: myList) { 
     total += x; 
     } 
     System.out.println("Total is " + total); 

     // Finding the largest element 
     double max = myList[0]; 
     int m, z = 0; 

     for (double k: myList) { 
     if (k > max) max = k; 
     } 

     do{ 
     for (int i = m; i < myList.length; i++) { 
      if (myList[i] > max){  
      max = myList[i]; 
      z = i; 
      } 
     } 

     sortedList[m] = max; 
     myList[z] =0; 
     m++; 

     } while(m < myList.length); 

     System.out.println("Max is " + max); 
     //System.out.println(Arrays.toString(myList)); 
     for (double y: sortedList) { 
     System.out.println(y + " "); 
     } 
    } 
} 
+0

看来,你从来没有分配任何东西到'sortedList',除了可能的第一个元素。 –

+0

@TimBiegeleisen实际上他似乎这么做,但由于意图不佳,很难发现。我会修复:) – Thomas

+0

@Thomas感谢您的编辑:-) –

您的排序逻辑无法按预期工作。我已经做了一些改动它,给它一个尝试:

do { 
      max = 0; 
      for (int i = 0; i < myList.length; i++) { 
       if (myList[i] > max) { 
        max = myList[i]; 
        z = i; 
       } 
      } 

      sortedList[m] = max; 
      myList[z] = 0; 
      m++; 

    } while (m < myList.length); 

,你可以简单地使用内置的功能,您按降序排列排序为

Arrays.sort(myList , Collections.reverseOrder()); 

System.out.println("myList Array Elements in reverse order:"); 
    for (int i = 0; i < myList .length; i++) 
     System.out.println(intArray[i]); 

这将肯定工作。

首先,你需要这条线int m, z = 0;转换为int m = 0, z = 0; 因为int m, z = 0;相当于int m; int z = 0;。因此,当您尝试使用变量m - 尚未初始化,并导致编译错误。 固定在上述语句之后,你的程序将编译和运行,但也有在程序逻辑一个错误,以及和你的结果来分类的阵列将被输出为:

{9.2, 9.2, 9.2, 9.2, 9.2, 9.2, 9.2}

如下面的块

for (double k: myList) { if (k > max) max = k; }

你最初发现最大值是9.2。这就是为什么当你以后执行do .. while这里检查车况

if (myList[i] > max){ max = myList[i]; z = i; }

声明myList[i] > max永远不会返回true,因此你max将始终保持9.2和z将始终保持0。这就是为什么行sortedList[m] = max;总是为您的排序数组的每个索引插入9.2。

在这种情况下,我建议您使用您选择的IDE(Intellij Idea,Eclipse等),它将突出显示编译错误并帮助您使用集成调试器查找错误。

所以我刚刚发现你的错误,我想现在你可以管理它。如果有其他帮助,可随时沟通。

以下代码适用于我。

public class Main { 

public static void main(String[] args) { 
    double[] myList = {1.9, 2.9, 9.2, 3.4, 4.2, 6.7, 3.5}; 
    double[] sortedList = new double[7] ; 


    // Print all the array elements 
    for (double i: myList) { 
     System.out.println(i + " "); 
    } 

    // Summing all elements 
    double total = 0; 
    for (double x: myList) { 
     total += x; 
    } 
    System.out.println("Total is " + total); 

    // Finding the largest element 
    double max = myList[0]; 
    int m = 0; 
    int z = 0; 

    do{ 
     for (int i = 0; i < myList.length; i++) { 
      if (myList[i] > max){ 
       max = myList[i]; 
       z = i; 
      } 
     } 

     sortedList[m] = max; 
     myList[z] =0; 
     m++; 
     max = 0; 

    } while(m < myList.length); 

    System.out.println("Max is " + max); 
    //System.out.println(Arrays.toString(myList)); 
    for (double y: sortedList) { 
     System.out.println(y + " "); 
    } 
} 
} 

你的代码包含了三个错误:

1.You失败在每次迭代重置“最大”,导致“排序列表” 只包含在每一个条目中的值9.2。

  1. for (double k: myList) { 
    if (k > max) max = k; 
    } 
    

    是不必要的。而且,它甚至没有跟踪最大元素的位置。

  2. 3.

    for (int i = m; i < myList.length; i++) 
    

    应改为

    for (int i = 0; i < myList.length; i++) 
    

    你在“排序列表”的位置无关,与在那里你可以找到 的最大元素'myList'。

开始=>