Design principle and implementation code example of iterator iterator in Java programming
We know that an iterator is an object that can be used to traverse some or all of the elements in a standard template library. So what is the design principle of iterator iterator? What does the iterator ask about defining an excuse instead of a class?
We assume that the iterator's function of iterating data is defined as a class, then there will be such a problem. Different collections have different data structures, so their storage methods are also different. In other words, when the iterator obtains, the method of obtaining is changed, that is, it is not fixed. Therefore, it is unreasonable to define this method as a specific implementation.
No matter what kind of collection, they must have the function of obtaining, and they don't know when there will be no data. All of them will also have judgment function. Therefore, the judgment and acquisition functions are extracted and defined in the interface. In this way, no matter which collection is traversed in the future, they only need to implement the iterator interface.
How did that happen? Collection iterators are implemented as inner classes. Simply look at a piece of code.
output
summary
The above is all about the design principle and implementation code example of Java programming iterator iterator in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java programming using socket multithreading to access server file code examples, Java multithreading programming to achieve socket communication example code, etc. if you have any questions, you can leave a message. Welcome to exchange and discuss. Thank you for your support for the programming tips website!