/* Test de l'utilisation des tableaux */ /* en parametre de fonction */ public class PassageTableauEnParametre { ///////////////////////////////////////////////// /* Passage d'un tableau en parametre */ /* Fonction d'affichage d'un tableau d'entiers */ /* t : Le tableau d'entiers à 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 et de l'adresse mémoire */ /* du tableau */ /* t : Le tableau d'entiers à afficher */ /* message : Le message préalable */ static void affichage(int [] t,String message) { Ecran.formater("%s : %11s : ",message,t); affichage(t); } /* Passage d'un tableau en parametre */ /* Modification du tableau a l'interieur */ /* de la fonction */ /* -> Modification du tableau sur lequel */ /* la fonction a ete appelee */ /* tab : Le tableau d'entiers à modifier */ static void testPassage(int [] tab) { int i; affichage(tab,"tab"); for ( i = 0 ; i < tab.length ; i = i+1 ) { tab[i] = 9; } affichage(tab,"tab"); } /* Retour d'un tableau par une fonction */ /* apres declaration et initialisation */ /* dans le corps */ /* n : La taille du tableau d'entiers à créer */ /* et retourner */ static int [] testRetour(int n) { int i; int [] tab = new int[n]; for ( i = 0 ; i < tab.length ; i = i+1 ) { tab[i] = i; } affichage(tab,"tab"); return tab; } /* Programme principal */ public static void main(String [] args) { int [] t1 = { 0,1,2,3,4,5,6,7 }; int [] t2; /* Test de la fonction testPassage */ Ecran.afficherln("Tableau passé en paramètre d'entête de fonction"); affichage(t1,"t1 "); testPassage(t1); affichage(t1,"t1 "); /* Test de la fonction testRetour */ Ecran.afficherln("Tableau passé en retour de fonction"); t2 = testRetour(10); affichage(t2,"t2 "); } ///////////////////////////////////////////////// }