demande d'explication sur un raisonement
j'ai fais un code pour trier des mots saisies au clavier dans l'ordre alphabétique
je l'ai commpléter avec un code que j'ai trouver dans mon livre pour utiliser la methode CompareTo()
Voilà le code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.crypto.spec.OAEPParameterSpec;
import javax.swing.text.StyledEditorKit.BoldAction;
public class TriDeMots
{
public TriDeMots()
{
int nombre = 0;
String mots[];
System.out.print("Combien de mots ? ");
Scanner s;// = new Scanner(System.in);
boolean isInt;
do{
isInt=true;
s = new Scanner(System.in);
try{
nombre = s.nextInt();
}catch(Exception e)
{
System.out.print("Veuillez saisir un entier : ");
isInt =false;
}
}while(isInt!=true);
mots = new String[nombre];
System.out.println("voulez saisir les "+nombre+" Mots SVP :");
for(int i=0;i<nombre;i++)
{
System.out.print("Le "+i+1+" mot :");
mots[i] = s.next();
}
String motMins [] = new String[nombre];
for(int i=0;i<nombre ; i++)
{
motMins[i] = mots[i].toLowerCase();
}
for(int i=0;i<nombre ; i++)
{
System.out.println(motMins[i]);
}
// tri par reorganisation des references (mots d'origine et en minuscules)
// (on compare chaque mot (minuscule) a tous ses suivants)
String temp;
for (int i=0 ; i<nombre-1 ; i++)
{
for (int j=i+1 ; j<nombre ; j++) // je fais un decalage entre les i et le j(i+1) pour comparer les reference .
{
if (motMins[i].compareTo(motMins[j]) >= 0)
{
temp = motMins[i] ;
motMins[i] = motMins[j] ;
motMins[j] = temp ;
temp = mots[i] ;
mots[i] = mots[j] ;
mots[j] = temp ;
}
}
}
System.out.println ("Liste par ordre alphabetique :") ;
for (int i=0 ; i<nombre ; i++)
System.out.println (mots[i]) ;
}
} |
mais la partie que j'ai récupéré du livre
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
String temp;
for (int i=0 ; i<nombre-1 ; i++)
{
for (int j=i+1 ; j<nombre ; j++) // je fais un decalage entre les i et le j(i+1) pour comparer les reference .
{
if (motMins[i].compareTo(motMins[j]) >= 0)
{
temp = motMins[i] ;
motMins[i] = motMins[j] ;
motMins[j] = temp ;
temp = mots[i] ;
mots[i] = mots[j] ;
mots[j] = temp ;
}
}
} |
je ne comprends pas ce raisonement
Code:
1 2 3 4 5
|
temp = motMins[i] ;
motMins[i] = motMins[j] ;
motMins[j] = temp ;
temp = mots[i] ; |
si qu'un peut m'éclairé un peut c'est trop floww!!