struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Laurie Harper <lau...@holoweb.net>
Subject Re: [OT] [JAVA] Iterate a collection and modify
Date Wed, 11 Apr 2007 22:17:31 GMT
First, this list is for Struts issues, not a general Java programming 
help. That said, here's a quick response:

Modifying an Iterator while iterating it is not guaranteed to work. It 
depends on the specific Iterator implementation. This is clearly 
documented in the Javadoc for the Collections APIs, so read those to 
find out if what you are doing is safe.

The general solution would be to take a copy of the iterator, iterate 
the copy and modify the original (or vica versa).

L.

temp temp wrote:
>     How can I  iterate  a collection  and modify at the same time ?
>       Example
>       public static Collection  getAncestors(
>   Entity aEntity, Object aContext ){
>          Collection vAncestors=new ArrayList();
>                   IDocument vDocument= findDocumentByName(
>   aEntity.getSuper(),  aContext);
>          vAncestors.add(vDocument);
>       for(
>   Iterator  i=vAncestors.iterator();
>   i.hasNext(); 
>   ){
>           getAncestors(vAncestors,  
>   (IDocument)i.next(), aContext);
>   } 
>           return vAncestors;
>   }
>             
>        public static void  getAncestors(
>                    Collection aAncestors, 
>                    IDocument   aDocument , 
>                    Object aContext){
>          Entity  vEntity=(Entity)aDocument.get();
>          if(vEntity.getSuper()!=null){
>            IDocument vDocument=    findDocumentByName(
>   vEntity.getSuper(),  
>   aContext);
>        aAncestors.add(vDocument);
>           }     
>   }
>        Thanks 
>   Miro
>     
>        
> ---------------------------------
> Sucker-punch spam with award-winning protection.
>  Try the free Yahoo! Mail Beta.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message