Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1528 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Iterator

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Iterator

n°533372
Shogun2002
Posté le 07-10-2003 à 16:22:54  profilanswer
 

Quand on convertit une HashMap en un iterator, de type :
 
maHash->HashMap=> avec un String comme clé et monObjet comme valeur
 
Iterator it = (Iterator)maHash.values().iterator();
 
C bien les Objet valeur (value) qui sont stocké dans l'iterator ?
 
Et pourquoi ensuite lorsque je fais un :
 
monObjet lobjet = (monObjet)it.next();
 
Il me sort une java.lang.ClassCastException
 
Donc je peux plus pas reprendre mon objet qui était dans ma HashMap ?
 

mood
Publicité
Posté le 07-10-2003 à 16:22:54  profilanswer
 

n°533375
darklord
You're welcome
Posté le 07-10-2003 à 16:28:17  profilanswer
 

:o
 
Quel est le but de  
 
Iterator it = (Iterator)maHash.values().iterator();  :??:
 

Citation :


C bien les Objet valeur (value) qui sont stocké dans l'iterator ?


 
non l'itérateur te permet d'accéder aisément à une collection qui contient tes objets, nuance
 

Citation :


Donc je peux plus pas reprendre mon objet qui était dans ma HashMap ?  


 
bin si [:spamafote]
 
tout dépend comment tu as construis ta hashmap déjà. Montre réellement le code que tu fais tourner pour voir déjà ...


Message édité par darklord le 07-10-2003 à 16:28:26

---------------
Just because you feel good does not make you right
n°533376
nerisson
Pic-pic
Posté le 07-10-2003 à 16:28:52  profilanswer
 

Chez moi ca marche:

Code :
  1. java.util.HashMap hm = new java.util.HashMap();
  2.     hm.put("clef", new Integer(1));
  3.     java.util.Iterator it = hm.values().iterator();
  4.     while(it.hasNext())
  5.       System.out.println(it.next().getClass().getName());


Donne nous un bout code complet et le message de l'exception, ca sera plus simple.


---------------
Light is right
n°533377
Shogun2002
Posté le 07-10-2003 à 16:29:25  profilanswer
 

En gros il veut pas me caster l'Objet en monObjet ... :sweat:

n°533379
Taiche
(╯°□°)╯︵ ┻━┻
Posté le 07-10-2003 à 16:31:40  profilanswer
 

Ba soit t'insères pas un monObjet dans ton insert de HashMap, soit tu nous dis pas tout :D


---------------
Everyone thinks of changing the world, but no one thinks of changing himself  |  It is the peculiar quality of a fool to perceive the faults of others and to forget his own  |  Early clumsiness is not a verdict, it’s an essential ingredient.
n°533383
darklord
You're welcome
Posté le 07-10-2003 à 16:33:55  profilanswer
 

Shogun2002 a écrit :

En gros il veut pas me caster l'Objet en monObjet ... :sweat:  


 
il veut pas :sweat:
 
:sarcastic:
 
mais pitié, achevez le quoi :/
 
tu vas le montrer ton code? La JVM elle fait ce que tu lui demandes hein (enfin pour des trucs aussi basiques)


---------------
Just because you feel good does not make you right
n°533386
Taiche
(╯°□°)╯︵ ┻━┻
Posté le 07-10-2003 à 16:38:38  profilanswer
 

DarkLord a écrit :


La JVM elle fait ce que tu lui demandes hein (enfin pour des trucs aussi basiques)


Sauf si elle est d'humeur taquine :o


---------------
Everyone thinks of changing the world, but no one thinks of changing himself  |  It is the peculiar quality of a fool to perceive the faults of others and to forget his own  |  Early clumsiness is not a verdict, it’s an essential ingredient.
n°533388
darklord
You're welcome
Posté le 07-10-2003 à 16:39:06  profilanswer
 

Taiche a écrit :


Sauf si elle est d'humeur taquine :o


 
par exemple :o
dans le cas d'un iterator et d'une hashmap j'ai comme un doute :o


---------------
Just because you feel good does not make you right
n°533389
LetoII
Le dormeur doit se réveiller
Posté le 07-10-2003 à 16:39:16  profilanswer
 

DarkLord a écrit :


 
il veut pas :sweat:
 
:sarcastic:
 
mais pitié, achevez le quoi :/
 
tu vas le montrer ton code? La JVM elle fait ce que tu lui demandes hein (enfin pour des trucs aussi basiques)


 
 :lol:  [:grinking]


---------------
Le Tyran
n°533392
Shogun2002
Posté le 07-10-2003 à 16:43:16  profilanswer
 

C du JSP
monMap étant la HashMap et GradGrille l'objet.
 
while (rs1.next()) {
%>
<%=
 rs1.getString("nomc" )
%>
<%
 classes.GradGrille v = new classes.GradGrille(rs1.getInt("id" ),rs1.getInt("grilleid" ),rs1.getInt("pos" ),rs1.getString("nomc" ),rs1.getString("noml" ),rs1.getFloat("valeur" ));
 monMap.put(String.valueOf(i),v);
 i++;
 }
%>
 
Ensuite  
<%
iteMap=monMap.values().iterator();  
%>
LA TAILLE<%= monMap.size() %>
<%
i=0;
while (i!=monMap.size()) {
 i++;
 classes.GradGrille monGrad = (classes.GradGrille)iteMap.next();
%>
<%=
monGrad.nomL
%>
 
 
Et l'erreur :
 
java.lang.ClassCastException
 at org.apache.jsp.grille_jsp._jspService(grille_jsp.java:132)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
 at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
 at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2415)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
 at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
 at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
 at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
 at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
 at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
 at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
 at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
 at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:223)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:594)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:392)
 at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:565)
 at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:619)
 at java.lang.Thread.run(Thread.java:536)
 

mood
Publicité
Posté le 07-10-2003 à 16:43:16  profilanswer
 

n°533394
Taiche
(╯°□°)╯︵ ┻━┻
Posté le 07-10-2003 à 16:46:40  profilanswer
 

[:mlc]
monMap.put(String.valueOf(i),v);
[...]
classes.GradGrille monGrad = (classes.GradGrille)iteMap.next();
 
Et tu t'étonnes qu'y a une ClassCastException ? T'entres une String et tu la castes en GradGrille, forcément ça se viande [:spamafote]


---------------
Everyone thinks of changing the world, but no one thinks of changing himself  |  It is the peculiar quality of a fool to perceive the faults of others and to forget his own  |  Early clumsiness is not a verdict, it’s an essential ingredient.
n°533396
Shogun2002
Posté le 07-10-2003 à 16:50:06  profilanswer
 

put(Object key, Object value)  
          Associates the specified value with the specified key in this map.
 
Bah c la clé ou la valeur qui est pris en compte dans l'iterateur ?

n°533400
LetoII
Le dormeur doit se réveiller
Posté le 07-10-2003 à 16:51:47  profilanswer
 

Taiche a écrit :

[:mlc]
monMap.put(String.valueOf(i),v);
[...]
classes.GradGrille monGrad = (classes.GradGrille)iteMap.next();
 
Et tu t'étonnes qu'y a une ClassCastException ? T'entres une String et tu la castes en GradGrille, forcément ça se viande [:spamafote]


 
 :heink:  
 
Je rappel:

Code :
  1. put(Object key, Object value)


Citation :


public Collection values()
 
Returns a collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.  


 


---------------
Le Tyran
n°533401
Shogun2002
Posté le 07-10-2003 à 16:52:02  profilanswer
 

J'ai essayé l'inverse et ça marche toujours pas

n°533403
Taiche
(╯°□°)╯︵ ┻━┻
Posté le 07-10-2003 à 16:52:58  profilanswer
 

Ah ouais, chu passé à côté du v, autant pour moi :o


---------------
Everyone thinks of changing the world, but no one thinks of changing himself  |  It is the peculiar quality of a fool to perceive the faults of others and to forget his own  |  Early clumsiness is not a verdict, it’s an essential ingredient.
n°533407
Taiche
(╯°□°)╯︵ ┻━┻
Posté le 07-10-2003 à 16:55:39  profilanswer
 

Pis juste un truc : fais pas ton while() sur la taille de ta HashMap, c'est pas terrible. T'as la méthode hasNext() sur ton Iterator, donc utilise-la puisque c'est sur ton Iterator que t'appelles next() et non sur la HashMap.


---------------
Everyone thinks of changing the world, but no one thinks of changing himself  |  It is the peculiar quality of a fool to perceive the faults of others and to forget his own  |  Early clumsiness is not a verdict, it’s an essential ingredient.
n°533412
Shogun2002
Posté le 07-10-2003 à 16:58:32  profilanswer
 

Taiche a écrit :

Pis juste un truc : fais pas ton while() sur la taille de ta HashMap, c'est pas terrible. T'as la méthode hasNext() sur ton Iterator, donc utilise-la puisque c'est sur ton Iterator que t'appelles next() et non sur la HashMap.


 
Ok

n°533418
Shogun2002
Posté le 07-10-2003 à 17:10:02  profilanswer
 

Ben maintenant ça marche ....
 
je sais pas ce que que j'ai touché .... :pt1cable:  
 
Mais en effacant et en reecrivant la meme chose, ça fonctionne !
 
Et désolé à ceux qui ont perdu leur temps sur mon post  :jap:

n°533423
the real m​oins moins
Posté le 07-10-2003 à 17:15:40  profilanswer
 

ben t'essayais de caster un null en tonObjet
 


---------------
Hey toi, tu veux acheter des minifigurines Lego, non ?
n°533424
the real m​oins moins
Posté le 07-10-2003 à 17:16:16  profilanswer
 

ça aurait marché avec while (i<monMap.size()) { ...


---------------
Hey toi, tu veux acheter des minifigurines Lego, non ?
n°533425
darklord
You're welcome
Posté le 07-10-2003 à 17:19:00  profilanswer
 

:sweat:


---------------
Just because you feel good does not make you right
mood
Publicité
Posté le   profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Iterator

 

Sujets relatifs
std :: list<float> :: iterator iy = c.coordx.beging()Un algo de tri, oui mais avec Iterator
remplir un vector avec un iterator 
Plus de sujets relatifs à : Iterator


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR