Ich habe über Java-Arrays gesucht und versucht, meine eigene SortArray() - Funktion zu erstellen, anstatt jemand anderen zu verwenden. Wenn ich das Array [1, 2, 3, 4, 5] an die Funktion SortArray() übergebe, sollte es ein weiteres Array [5, 4, 3, 2, 1] zurückgeben und das Array hoch zu niedrig sortieren. Stattdessen gibt die Funktion das exakt gleiche Array wie das Array in den Parametern zurück. Ich habe kommentiert, was jeder Codeblock im Code tun sollte, lass es mich wissen, wenn du etwas findest!Warum kann diese benutzerdefinierte sortarray-Funktion das Array nicht sortieren?
public static int[] sortArray(int[] array) {
//Declare the highest value found so far
int highest;
//loop through every index in the array
for (int minIndex = 0; minIndex < array.length; minIndex++) {
//Set the current highest value to the first index
highest = array[minIndex];
//loop through every index past the minimum to check if there is a higher numer
//do not check the indexes before the minIndex
for (int i = 0 + minIndex; i < array.length; i++) {
//Check to see if the current index is higher than the highest
//if so, make that value the new highest and loop again from the next index
if (array[i] > highest) {
highest = array[i];
minIndex ++;
}
}
}
return array;
}
einfach, Sie aktualisieren nie das Array. – Robert
Wo ändern Sie das Array? Alles, was Sie tun, ist die Änderung von "höchst" und "minIndex", was die for-Schleife-Variable ist, so dass Sie das nicht ändern sollten – Tyler