[英]JAVA: Sorting an array in descending order
我正在嘗試以降序對數組進行排序,我知道有很多在線進行數組排序的示例,但是我只是想嘗試以自己的方式進行操作(只是嘗試測試算法是否可以正常工作)。 但是由於某些原因,我無法輸出存儲結果的數組,我嘗試使用System.out.println(Arrays.toString(myList)); 並一次打印一次,它適用於我創建的數組之一,但是當嘗試通過循環修改數組時,它拒絕輸出任何內容,沒有錯誤,沒有任何內容,好像沒有任何內容一樣。 您的幫助將不勝感激。 參見下面的代碼。 謝謝。
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 + " ");
}
}
}
您可以簡單地使用內置函數將數組按降序排序
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]);
它肯定會工作。
您的排序邏輯未按預期工作。 我已經對其進行了一些更改,請嘗試一下:
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);
首先,您需要將此行轉換為int m, z = 0;
到int m = 0, z = 0;
因為int m, z = 0;
等價於int m; int 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等),該IDE將突出顯示編譯錯誤並幫助您使用集成的調試器查找錯誤。
所以我剛剛發現了您的錯誤,我想現在您可以解決它。 如果需要其他幫助,請隨時交流。
以下代碼對我有用。
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.您沒有在每次迭代中重置“ max”,從而導致“ sortedList”在每個條目中僅包含值9.2。
for (double k: myList) { if (k > max) max = k; }
是不必要的。 而且,它甚至不跟蹤max元素的位置。
3。
for (int i = m; i < myList.length; i++)
應該更改為
for (int i = 0; i < myList.length; i++)
您在“ sortedList”中所處的位置與可以找到“ myList”的最大元素的位置無關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.