iterator - Modifying a set during iteration java -


I'm looking to repeat a recursive method.

I have an object that I want to repeat again, and then check all my sub-bases.

recursive:

  while twofunction (object) (iterator.hasNext ()) {// doStuff doFunction (Object.subObjects); }  

I want to change it to do something like this

  doFunction (object) iiterator = hashSet.iterator (); While (Iterator.hasNext () {// doStuff hashSet.addAll (objects. SubObjects)}}  

Sorry for the poor psuedo code, but basically I'm on the sub-topic again I want to add new ones to check the objects at the end of the list.

I can do this using the list, and

  do anything like (List.size ()> gt0) {// doStuff list.addAll (Object.subObjects);}  

But I really do not want to add duplicate sub-objects. Of course I just Can check I know whether list.contains (each sub-object) before I added it.

But I would like to use a set to complete that cleaner.

Actually there is a way to add a set,

Any comment is appreciated.

Thanks

>

I use two data structures --- a queue whose subobjects are to be visited, and a < Strong> set ( Ase. ) To store all visited objects without duplicates.

  visited = new hashset (); // All visited objects query next = new ArrayDek (); // objects whose subobjects are to be visited // Note: In all the times, the objects in the "next" are included in "visits" / visited the first object. Add (obj); Object nextObject = obj; While (nextObject! = Null) {// dofor next object (object o: nextObject.subobjects) {boolean fresh = visited.add (o); If (fresh) {next.add (o); }} Next object = Next Paul ();  

Note: Note that the " / P>

  • A space-efficient queue is applied as a cyclic array, which means that you use less space than when you add elements while adding them.
  • " Boolean fresh = visited. Ed (o) " Combined ".

Comments