/* Tests d'egalite entre 2 tableaux */ /* Implantation avec utilisation de fonctions */ public class TestEgaliteTableauxAvecFonctions { ////////////////////////////////////////////////// /* Fonction d'affichage d'un tableau d'entiers */ /* t : Le tableau de int a afficher */ static void affichage(int [] t) { int i; for ( i = 0 ; i < t.length ; i = i+1 ) { Ecran.afficher(t[i]," "); } Ecran.sautDeLigne(); } /* Fonction d'affichage d'un tableau d'entiers */ /* avec affichage préalable d'un message */ /* alphabétique */ /* message : La chaine a afficher préalablement */ /* t : Le tableau de int a afficher */ static void affichage(String message,int [] t) { Ecran.formater("%s : ",message); affichage(t); } /* Fonction de test de l'egalite */ /* de deux tableaux d'entiers et de retour */ /* du booleen true si c'est le cas, false sinon */ /* t1 : Le premier tableau de int du test */ /* t2 : Le second tableau de int du test */ static boolean testEgalite(int [] t1,int [] t2) { boolean egal = true; int i; if ( t1.length != t2.length ) { egal = false; } else { i = 0; while ( ( egal == true ) && ( i < t1.length ) ) { if ( t1[i] != t2[i] ) { egal = false; } i = i+1; } } return egal; } /* Programme principal */ public static void main(String [] args) { boolean egal; /* Declaration et initialisation de trois */ /* tableaux: t1, t2 et t3 */ int [] t1 = { 0,1,2,3 }; int [] t2 = { 0,0,0,0 }; int [] t3 = { 0,1,2,3 }; /* Affichage du contenu des trois tableaux */ affichage("t1",t1); affichage("t2",t2); affichage("t3",t3); /* Tests d'egalite par utilisation du == */ egal = (t1 == t2); Ecran.afficherln("Test d'egalite entre t1 et t2 avec == : ",egal); egal = (t1 == t3); Ecran.afficherln("Test d'egalite entre t1 et t3 avec == : ",egal); /* Tests d'egalite par implantation */ /* du test composante par composante */ egal = testEgalite(t1,t2); Ecran.afficherln("Test d'egalite entre t1 et t2 avec fonction : ",egal); egal = testEgalite(t1,t3); Ecran.afficherln("Test d'egalite entre t1 et t3 avec fonction : ",egal); /* Affichage des adresses mémoire */ /* des trois tableaux */ Ecran.afficherln("t1 : ",t1); Ecran.afficherln("t2 : ",t2); Ecran.afficherln("t3 : ",t3); } ////////////////////////////////////////////////// }