简体   繁体   中英

Difference between a for-loop and a while-loop using an Iterator

Iterator using while-loop:

List<DataType> list = new ArrayList<DataType>();  
Iterator<YourDataType> it = yourList.iterator();  
while (it.hasNext())   
   // Do something

Iterator using for-loop:

   List<DataType> list = new ArrayList<DataType>();
   for ( Iterator<DataType> it = list.iterator(); list.hasNext(); ) 
      // Do something

I've read that the for-loop minimizes the scope of the Iterator to the loop itself. What exactly does that mean? Should I use the for-loop insteed of the while-loop?

The difference is basically

In for loop:

for (Iterator<DataType> it = list.iterator(); list.hasNext(); ) 

In this case, you are declaring the Iterator object locally and it will be eligible for GC(garbage collection) after the for loop .

In while loop ,

while (it.hasNext()) since you have declared the object Iterator outside a loop. So its scope is may be the entire program or the method it is in . Or incase if it is referenced anywhere, so it wont be eligible for GC .

Hope this helps.

the for-loop minimizes the scope of the Iterator to the loop itself. What exactly does that mean?

To use the Iterator on the while loop, you must declare and initialize it before being using in the while loop. So, you can do this:

List<DataType> list = new ArrayList<DataType>();  
Iterator<YourDataType> it = yourList.iterator();  
while (it.hasNext()) {
}
it.hasNext(); //this compiles and will return false

In the for loop, the Iterator is declared to be inside the scope of the for loop, so it can be used only inside the body of this loop.

List<DataType> list = new ArrayList<DataType>();
for ( Iterator<DataType> it = list.iterator(); list.hasNext(); ) {
}
it.hasNext(); //this won't work and is a compiler error since the variable it's outside its scope

Should I use the for-loop insteed of the while-loop?

This entirely depends on your needs. Usually, if you're going to consume all the elements of the iterator in a single loop, it is better to use the for loop approach, and it will be better using the enhanced for loop that already uses Iterator behind the scenes:

for(DataType data : list) {
    //...
}

In cases where you won't navigate through all the elements of the iterator in the loop, it would be better to use a while . An example of this could be implementing a merge sort (Note: this example is entirely for learning purposes).

Because you declare the iterator inside the for loop, you won't be able to use it outside of it. Its scope will be limited to the for-loop block.

Use what makes more sense in your case. Do you need the iterator after you've iterated over all the elements in your collection? Use the while-loop. If you don't care for it, use the for-loop.

Note that when things go out of scope, they become eligible for garbage collection.

There are two important differences in syntax and maintenance.

Syntax

The scope of the variable is different. In the for case, the variable is only accessible within the for header and body, while in the while case it is available after the loop. As a general rule, it's better to have tighter scopes, less variables in-flight mean less context to worry about when coding.

Maintenance

The for loop has the neat advantage of grouping all the iterations operations close together, so they can be inspected in one shot and so checked.

Apart from this:

for loops are more readable and all-around better for regular iteration patterns where as while loops should be reserved to irregular iteration patterns

As JNL said, with a limited scope the garbage-collection can collect it directly after the loop instead of until the next } .

Normally this is of small concern - an iterator doesn't really take up much memory and the scope normally ends pretty quickly after the iterator is used. For instance, if you put both your code samples in a method, they will yield the same scope. Another way to limit the scope of the while loop is to add curly brackets around it, as such:

{
   List<DataType> list = new ArrayList<DataType>();  
   Iterator<YourDataType> it = yourList.iterator();  
   while (it.hasNext())   
      // Do something
}

This will also limit the scope of list , which is more heavy in memory usage than the iterator and thus more relevant.


TL;DR

Doesn't matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM