Warum scheint meine Sortierschleife ein Element an eine Stelle anzuhängen, an der es nicht sein sollte?

Ich versuche, eine Reihe von Zeichenfolgen mit zu sortierencompareTo(). Das ist mein Code:

static String Array[] = {" Hello ", " This ", "is ", "Sorting ", "Example"};
String temp;

public static void main(String[] args)
{

   for (int j=0; j<Array.length;j++)
   {
       for (int i=j+1 ; i<Array.length; i++)
       {
           if (Array[i].compareTo(Array[j])<0)
           {
               String temp = Array[j];
               Array[j] = Array[i];
               Array[i] = temp;
           }
       }
       System.out.print(Array[j]);
   }
}

Jetzt ist die Ausgabe:

Hello  This Example Sorting is

Ich erhalte Ergebnisse, aber nicht die Ergebnisse, die ich erzielen möchte:

Hello This Example Is Sorting

Wie kann ich meinen Code anpassen, um das String-Array richtig zu sortieren?

Antworten auf die Frage(8)

Ihre Antwort auf die Frage