Bonjour
J'ai une question à vous poser: est ce que quelqu'un peut m'aider?
Enoncé
Réécrire la méthode de tri par sélection en utilisant l'interface Comparable définie dans l'API JAVA. Ecrire une fonction principale qui permet de trier un tableau d'entier
Voilà ce que j'ai fait
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
| import java.util.*;
public class Triparsel implements Comparable{
private Comparable obj;
private Comparable []tab;
public Triparsel(Comparable obj){
this.obj=obj;
}
public int compareTo(Object o){
if(o instanceof Comparable){
Comparable nouv= (Comparable)o;
return this.obj.compareTo(nouv);
}
return -1;
}
public Comparable [] Tri (Comparable [] tab){
Comparable aux;
for (int i=0; i<tab.length; i++){
for (int j=1; j<tab.length; j++){
if(tab[i].compareTo(tab[j])>0){
aux= tab[i];
tab[i]=tab[j];
tab[j]=aux;
}
}
}
return tab;
}
public static void main (String[]args){
int[] tab= { 1, 2,4, 3};
int [] triee =Tri(tab);
for(int i=0; i<tab.length; i++){
System.out.println(triee [i]);
}
} |
Problème (dans la méthode main)
1 2
| Tri(Comparable[]) in Triparsel cannot be applied to (int[])
int [] triee =Tri(tab); |
Pouvez vous m'aidez svp? (car ce problème ce n'est pas la première fois que ça marrive... et j'ai jamais su y résoudre)
Merci
Partager