About next() of java iterator

Table of contents

Kind tips

Basic use of java iterators

exploration part

1.

2.

in conclusion


 

Kind tips

Before reading this article, it is best to understand Java collections, the concept and basic usage of iterators. .

This article is a conclusion drawn from the author's personal whims and explorations, and is for reference only. If you are too lazy to read the process, you can jump directly to the conclusion.

For iterator learning, please refer to http://t.csdn.cn/bK94R

Basic use of java iterators

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author 
 */
public class Op {
public static void main(String[] args) {
     ArrayList <Integer> it=new ArrayList<>();
     it.add(5);
     it.add(13);
     it.add(25);
     it.add(26);
     it.add(28);
     Iterator <Integer> ui= it.iterator();
     System.out.println(it);
     
     while(ui.hasNext()){
System.out.print(ui.next()+" ");
ui.remove();

     }
System.out.println();
System.out.println(it);
     }
}

Output result:

[5, 13, 25, 26, 28]
5 13 25 26 28 
[]

 

exploration part

1.

When I saw the above code outputting and deleting the collections in sequence, I thought about whether I could use the judgment statement to only output and delete some elements in the collection, so I slightly modified the while judgment statement.

while(ui.hasNext()&&ui.next()<24)

Try running it

[5, 13, 25, 26, 28]
13 
[5, 25, 26, 28]

The output result is a bit confusing. Why is it that when outputting elements in sequence, only 13 is output? Where did 5 go?

2.

I try to make a hypothesis first: ui.next() in while(ui.hasNext()&&ui.next()<24) and ui in System.out.print(ui.next()+" "); The elements corresponding to .next() are different.

Then I modified the code again and tried to insert a ternary operator to check

while (ui.hasNext()) {
         System.out.print(((ui.next() <= 24) ? ui.next() : 2) + " ");
         ui.remove();

      }

Output result:

[5, 13, 25, 26, 28]
13 2 2 2
[5]

The output results just now both output elements starting from 13, and then output 2 if subsequent elements do not meet the conditions.

Hypothesis again: I guess that although in the same code , System.out.print(((ui.next() <= 24) ? ui.next() : 2) + " "); both ui.next() The elements referred to are also different

Under what circumstances can the elements pointed to by two ui.next() be the same? You might as well set up a variable for assignment

         int b = ui.next();
         System.out.print(((b <= 24) ? b : 2) + " ");

Output after running again:

[5, 13, 25, 26, 28]
5 13 2 2 2
[]

This time, 5 is output, and the conclusion can be drawn.

in conclusion

When using iterators, the elements represented by each next() are different and will change sequentially.

If next() is used in the conditional statement of the loop, then when next() is used again in the loop body statement, next() represents the next element.

If you want to avoid this situation and make two or more next() represent the same element, you can use a variable to represent next() so that the judgment and output next() represent the same element.

Coding is not easy. If it helps, I hope to leave a like and leave.

If there are any deficiencies in the article, please point them out in the comment area.

 

 

Guess you like

Origin blog.csdn.net/m0_72684053/article/details/129786488