Affichage d'une valeur tableau
Bonsoir à tous et Bonne Année,
Je voudrais afficher une valeur tableau :
Code:
1 2 3 4 5
|
for (int i=0;i<tabTrie.length;i++)
{
System.out.println(tabTrie[i]);
} |
où tabTrie est
Code:
1 2
|
tabTrie=(Personne[])Personne.tri(tab); |
et tri est :
de la Classe Personne
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public static Classable[] tri(Classable[] tablo)
{
int i,j;
Classable c;
for (i=0;i< tablo.length;i++)
{
for( j = i + 1; j<tablo.length;j++)
{
if (tablo[j].compare(tablo[i])==Classable.INFERIEUR)
{
c = tablo[j];
tablo[j] = tablo[i];
tablo[i] = c;
}
else if (tablo[j].compare(tablo[i])==Classable.ERREUR)
{
return null;
}
}
}
return tablo;
} |
Le tri se fait bien
Classable est une interface implémentée par la Classe Personne :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public interface Classable
{
//cette méthode pourra être appelée pour comparer linstance
//courante avec celle reçue en paramètre
//la méthode retourne un entier dont la valeur dépend
//des règles suivantes
//1 si linstance courante est supérieure à celle reçue
//en paramètre
//0 si les deux instances sont égales
//-1 si linstance courante est inférieure à celle reçue
//en paramètre
//-99 si la comparaison est impossible
int compare(Object o);
public static final int INFERIEUR=-1;
public static final int EGAL=0;
public static final int SUPERIEUR=1;
public static final int ERREUR=-99;
} |
quand j'exécute :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
Personne[] tab;
tab=new Personne[5];
tab[0] = new Personne("toto2", "prenom2",new
GregorianCalendar(1922,2,15));
tab[1] = new Personne("toto1", "prenom1 ",new
GregorianCalendar(1911,1,15));
tab[2] = new Personne("toto5", "prenom5 ",new
GregorianCalendar(1955,05,15));
tab[3] = new Personne("toto3", "prenom3 ",new
GregorianCalendar(1933,03,15));
tab[4] = new Personne("toto4", "prenom4 ",new
GregorianCalendar(1944,04,15));
Personne[] tabTrie;
tabTrie=(Personne[])Personne.tri(tab);
for (int i=0;i<tabTrie.length;i++)
{
System.out.println(tabTrie[i]);
} |
s'affiche :
Cours.Personne@6a2bcfcb
Cours.Personne@4de8b406
Cours.Personne@3c756e4d
Cours.Personne@7c0e2abd
Cours.Personne@48eff760
J'ai essayé avec toString mais ç a ne marche pas
Si quelqu'un a une idée MErci