Difference between for-loop and while-loop using iterator

An iterator using a while-loop:

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

An iterator using for-loop:

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

I read that for-loop minimizes the scope of Iterator in the loop itself. What exactly does this mean? Should I use in-loop for while loop?

+6
source share
5 answers

The difference is mostly

In for loop:

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

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

In a while loop ,

while (it.hasNext()) , since you declared the Iterator object outside the loop. Thus, the area may be the entire program or method it is in . Or if it is referenced anywhere, so it will not be eligible for the GC .

Hope this helps.

+6
source

for-loop minimizes the scope of the Iterator in the loop itself. What exactly does this mean?

To use an Iterator in a while , you must declare and initialize it before using the while . 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 a for loop, an Iterator declared inside the scope of the for loop, so it can only be used 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 outside its scope 

Should I use in-loop for while loop?

It totally depends on your needs. Usually, if you are going to use all the elements of an iterator in a single loop, it is better to use the for loop approach, and it is better to use the extended for loop, which already uses Iterator behind the scenes:

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

In cases where you will not move through all the elements of the iterator in a loop, it would be better to use while . An example of this is the implementation of merge sorting (Note: this example is entirely for training).

+1
source

Since you are declaring an iterator inside a for loop, you cannot use it outside of it. Its volume will be limited by the for-loop block.

Use what makes more sense in your case. Do you need an iterator after you have repeated all the elements in your collection? Use a while loop. If you don't like this, use the for loop.

Please note that when things go beyond, they become suitable for garbage collection.

+1
source

There are two important differences in syntax and maintenance.

Syntax

The volume of the variable is different. In the case of for variable is available only inside the header and body, and in the case of while it is available after the loop. As a rule, it is better to have narrower areas, fewer variables in flight mean less context to worry about when coding.

Service

for loop has the neat advantage of grouping all iteration operations close together, so they can be checked with one shot and therefore checked.

Besides:

Cycles

for more readable and universal for regular iteration patterns, where irregular iteration patterns should be reserved in the while

+1
source

As JNL said, with a limited volume, garbage collection can collect it immediately after the cycle, and not until the next } .

This is usually of little interest - the iterator does not actually take up much memory, and the scope usually ends fairly quickly after using the iterator. For example, if you put both code samples in a method, they will have the same scope. Another way to limit the scope of the while loop is to add curly braces to it, as such:

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

This will also limit the list area, which is more difficult to use than the iterator, and therefore more relevant.


TL DR

It does not matter.

+1
source

Source: https://habr.com/ru/post/951643/


All Articles