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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| public class TriChaine2{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] tab={"tata","toto","titi","tatalo","dadi"};
do{
for (int i=0;i<tab.length-1;i++){
if(Ordonner(tab,i)==false)
Permute(tab,i);
}//Fin for i
}while(ListeOrdonner(tab)==false);
Afficher(tab);
} //fin main
/*
* ---------------------------------------------------------
* Cette méthode prend en parametre un tableau de strings
* et les compare deux à deux , elle renvoie
*true si les deux chaines sont alphabétiquement ordonnée;
*false sinon.
*--------------------------------------------------------------
*/
static boolean Ordonner(String[] tab,int i){
for (int k=0;k<tab.length-1;k++){
/*
* si la longueur de tab[i]est plus petit que celle de tab[i+1],
* on l'utilise dans la boucle for pour la compaison caractère
* par caractère
*/
if(tab[i].length()<=tab[i+1].length()){
for (int j=0;j<tab[i].length();j++){
if ((tab[i].charAt(j)<=tab[i+1].charAt(j))==false){
return false;
}// fin if
} // Fin for j;
}// fin if
/*
* sinon on utilise tab[i+1].
*/
else{
for (int j=0;j<tab[i+1].length();j++){
if ((tab[i].charAt(j)<=tab[i+1].charAt(j))==false){
return false;
}// fin if
} // Fin for j;
}// fin else
}// fin for i
return true;
}// fin Ordonner
/*
*---------------------------------------------------------------------
*Cette methode prend en parametre un tableau de string et
*permute, sous condition, deux chaines consecutives
*---------------------------------------------------------------------
*/
static void Permute(String[] tab, int i){
String C;
C=tab[i];
tab[i]=tab[i+1];
tab[i+1]=C;
}//Fin Permute
/*
* ------------------------------------------------------------
* * Cette methode affiche un tableau de string
* -------------------------------------------------------------
*/
static void Afficher (String[] tab){
for(String str:tab)
System.out.println(str);
}// Fin Afficher
/*
* --------------------------------------------------------------
* Cette methode prend en parametre un tableau de strings
* et vérifie si elle est alphabétiquement ordonnée
* -----------------------------------------------------------------
*/
static boolean ListeOrdonner(String[] tab){
for (int k=0;k<tab.length-1;k++){
if(Ordonner(tab,k)==false)
return false;
}// fin for
return true;
}// fin ListeOrdonner
} |