How to turn Iterator Dojo (Java)

Use Iterator

Iterator can be retrieved In most cases, the owner is Iterable, so it is possible to loop with an extended for statement without using Iterator (code below). This time, instead of using the extended for statement, I dare to use Iterator to run the loop. Thank you for your cooperation.

List is Iterable, so you can dig into an extended for statement


//List object to retrieve elements one by one
List<String> list = Arrays.asList("a", "i", "u", "e", "o"); 

//It seems that you can turn the loop by using something called Iterator, so take it out
Iterator<String> iterator = list.iterator(); 

//That being said, List is Iterable, so you can dig into an extended for statement.
for(String str : list){ 
  System.out.println(str);
}

//The iterator has been dismissed(Crying)
iterator = null;

ʻIterator `main method

Method function
T next() Extract the next element
boolean hasNext() Seen from the current state, true is returned if the next element can be retrieved.

Iterator is an interface for tracing elements in sequence. You can retrieve the element by calling the next () method. The general usage is "continue to callnext ()only whilehasNext ()returns true".

Iterator has an internal state, and each time you retrieve an element using next (), its internal state changes. (Finally, hasNext () will return false) It is a ** disposable premise ** interface because there are no defined methods to undo or return to the initial state.

(I don't think so) Please be careful when you make your own Iterator.

Code using Iterator

The introduction has become long, but it is a code. There are two types, a while statement and a for statement.

How to turn with a while statement

Iterator<String> itr = list.iterator();
while(itr.hasNext()){
  String str = itr.next();
  System.out.println(str);
}

How to turn by for statement

for(Iterator<String> itr = list.iterator(); itr.hasNext();){
  String str = itr.next();
  System.out.println(str);
}

Personally, I prefer the for statement, which narrows the scope of variables.

Recommended Posts

How to turn Iterator Dojo (Java)
How to turn off ChromeDriver notification bar (Java)
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to use java Optional
How to minimize Java images
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to set Java constants
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to study Java Silver SE 8
How to use Java HttpClient (Get)
[Java] How to update Java on Windows
How to disassemble Java class files
[Java] How to use join method
How to learn JAVA in 7 days
[Processing × Java] How to use variables
[Java] How to create a folder
How to decompile java class files
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to name variables in Java
How to pass Oracle Java Silver
java Eclipse How to debug javaScript
For Java beginners: List, Map, Iterator / Array ... How to convert?
[Processing × Java] How to use arrays
How to make a Java array
How to use Java lambda expressions
[Java] How to use Math class
How to find Java prime numbers
How to use Java enum type
How to concatenate strings in java
How to check Java installed on Mac
How to make a Java calendar Summary
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
How to compile Java with VsCode & Ant
[Java] How to use the hasNext function
[Java] How to compare with equals method
[java] Summary of how to handle char
[Java] How to add data to List (add, addAll)
How to use submit method (Java Silver)
[Java] How to use the HashMap class
How to do base conversion in Java
[Easy-to-understand explanation! ] How to use Java instance
[Introduction to Java] How to write a Java program