The Artima Developer Community
Sponsored Link

Java Answers Forum
iterator infinite loop

2 replies on 1 page. Most recent reply: Dec 5, 2004 7:00 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
stephen handley

Posts: 3
Nickname: one2one3
Registered: Dec, 2004

iterator infinite loop Posted: Dec 4, 2004 10:25 AM
Reply to this message Reply
Advertisement
hi,
i've been looking at this for awhile and i can't manage to see what i've done wrong. ..i have the following code i'm using to flatten a directory structure:

public ArrayList flatten(MyDir m) {
ArrayList fl = new ArrayList();
if (m != null) {
ArrayList mf = mdir.getFiles();
Iterator e = mf.iterator();
int cou = 0;
while(e.hasNext()) {
cou++;
System.out.println("flatten:e.hasNext(): " + e.hasNext());
MyFile nxt = (MyFile)e.next();
System.out.println("" + cou + ".flatten,adding " + nxt.getPath());
fl.add(nxt);
System.out.println("added.");
System.out.println("flatten:e.hasNext(): " + e.hasNext());
}
ArrayList md = mdir.getDirs();
e = md.iterator();
while (e.hasNext())
fl.addAll(flatten((MyDir)e.next()));
}
return fl
}

the problem is when i hit an infinite loop in the first iterator loop, for the ArrayList mf's iterator, i get as output:

flatten:e.hasNext(): true
1.flatten,adding /Users/stephen/Desktop/TICPP-2nd-ed-Vol-one/.DS_Store
added.
flatten:e.hasNext( ): false
flatten:e.hasNext(): true
1.flatten,adding /Users/stephen/Desktop/TICPP-2nd-ed-Vol-one/.DS_Store
added.
flatten:e.hasNext( ): false
flatten:e.hasNext(): true
1.flatten,adding /Users/stephen/Desktop/TICPP-2nd-ed-Vol-one/.DS_Store
added.
flatten:e.hasNext( ): false

i don't understand why its stuck in the first loop, relooping forever.

thanks for any help!
Stephen


stephen handley

Posts: 3
Nickname: one2one3
Registered: Dec, 2004

Re: iterator infinite loop Posted: Dec 5, 2004 7:30 AM
Reply to this message Reply
sorry, i figured this out, it was scope...i forgot to rename mdir to m when i changed the flatten() into a call to a helper function flatten(MyDir m), as flatten(mdir).

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: iterator infinite loop Posted: Dec 5, 2004 7:00 PM
Reply to this message Reply
FYI, to avoid flattening the code in your post, you can use the java tags described in the "Formatting Your Post" sidebar, visible while you are composing it. So instead of stuff like this:
class Ugly {
private bool isUgly = true;
public boolean ew() {
if( this.isUgly )
return true;
}
}
You get stuff like this:
class NotSoUgly {
   private bool isUgly = false;
   public boolean ew() {
      if( this.isUgly )
          return true;
   }
}

Flat View: This topic has 2 replies on 1 page
Topic: What's the name of that group box thingamajig Previous Topic   Next Topic Topic: Checking the arrays then adding and averaging...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use