mey89
Messages postés1Date d'inscriptionvendredi 15 mai 2009StatutMembreDernière intervention15 mai 2009
-
15 mai 2009 à 07:43
kirua12
Messages postés1155Date d'inscriptionsamedi 17 janvier 2004StatutMembreDernière intervention29 avril 2011
-
15 mai 2009 à 16:16
bonjour, j'ai une classe suivante de mot-clés :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package PFiches;
import java.util.*;
/**
*
* @author Aurélie
*/
public class Cmotcle {
//déclaration des attributs
private String mot_cle;
//liste des photos associées à un mot-clé
private LinkedList<Cphoto> photos = new LinkedList<Cphoto>();
//arbre contenant tous les mots-clés
private static ComparateurMC comp = new ComparateurMC();
private static TreeSet motcle = new TreeSet(comp);
//déclaration du constructeur lorsqu'un nouveau mot-clé est associé à une photo
public Cmotcle (String mot, Cphoto photo_taggée){
mot_cle=mot;
photos.add(photo_taggée);
motcle.add(this.mot_cle);
}
//déclaration du constructeur lors du lancement du programme
public Cmotcle(String mot){
mot_cle=mot;
motcle.add(this.mot_cle);
}
//////////////////////Déclaration des méthodes
//méthode permettant de retourner le titre du mot-clé
public String GetTitle(){
return(this.mot_cle);
}
//Méthode d'ajout d'une photo dans la liste du mot-clé
public void Ajout_photo(Cphoto photo){
photos.add(photo);
}
//Méthode de retrait d'une photo de la liste d'un mot-clé
public void Retirer_photo(Cphoto photo){
photos.remove(photo);
}
//Recherche d'une photo par son titre
public Cphoto Rechercher_photo(String tit){
//déclaration d'un itérateur pour parcourir la liste de photos
Iterator iter;
iter = photos.iterator();
while (iter.hasNext() ){
Cphoto photo =(Cphoto) iter.next();
if ((photo.get_titre()).equals(tit)){
return(photo);
}
}
return(null);
}
public void ajout_mc_arbre (){
motcle.add(this.mot_cle);
}
}
J'ai donc en instance de classe un arbre d'objets mot-clés.
Voici mon implémentation de la classe comprator :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package PFiches;
import java.util.Comparator;
/**
*/
public class ComparateurMC implements Comparator {
public int compare(Object o1, Object o2) {
//méthode permettant de comparer deux mot-clés
//renvoie 0 si mc1==mc2 et un entier positif si mc1>mc2
String mc1=((Cmotcle)o1).GetTitle();
String mc2=((Cmotcle)o2).GetTitle();
return mc1.compareTo(mc2);
}
}
Seulement dans ma classe Cmotcle la ligne de code :
Cmotcle mocle = (Cmotcle) iter.next(); ne fonctionne pas et bloque ainsi tout mon programme.
Voici l'erreur que l'annonce netbeans :
init:
deps-jar:
compile:
run:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to PFiches.Cmotcle
at PFiches.Cmotcle.existence_mc(Cmotcle.java:71)
at PFiches.Cphoto.(Cphoto.java:41)
at PFiches.Cphoto.lecture_photo(Cphoto.java:126)
at PFiches.CFAccueil.(CFAccueil.java:40)
at PFiches.CFAccueil$6.run(CFAccueil.java:234)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Pourriez vous m'éclairer? Je ne vois pas pourquoi le transtypage ne marche pas...
kirua12
Messages postés1155Date d'inscriptionsamedi 17 janvier 2004StatutMembreDernière intervention29 avril 20117 15 mai 2009 à 16:16
Salut,
le message d'erreur est clair : tu as un String au lieu de la classe Cmotcle.
Dans ton Set motcle, tu ajoutes que des String, tu ne peux pas les caster en Cmotcle.
Il est donc normal d'avoir un classCast lors du parcours. Soit tu ajoutes des String et tu parcours sur des String, soit tu ajoutes des Cmotcle.
Pense à typer tes collections :
private static Set<Cmotcle> motcle = new TreeSet<Cmotcle>(comp);