In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use iterator". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Read the following code and write out the output of this code:
Import java.util.ArrayList; import java.util.Iterator; import java.util.*; public class Test {public static void main (String [] args) {List list = new ArrayList (); list.add ("1"); list.add ("2"); list.add ("3"); Iterator iterator = list.iterator () While (iterator.hasNext ()) {String str = (String) iterator.next (); if (str.equals ("2")) {iterator.remove ();}} while (iterator.hasNext ()) {System.out.println (iterator.next ()) } System.out.println ("4");}}
The answer he wrote was:
1 3 4
The strange thing is that you send this question to the people around you and ask them to answer what the output of this interview question is, and there are a lot of people who say it. No, you try.
The answer is obviously wrong, because after the iterator.hasNext () = false in the first while will come to the second while, the same Iterator object, adjust iterator.hasNext () = = false once before, and judge again, the result is still the same?
So the second while is judged to be false, and it will no longer traverse the iterator, so the ontology answer is: 4.
Let's analyze why it is and how it is implemented at the bottom.
What is the Iterator here?
Iterator is a kind of pattern, and its design pattern can be seen in detail, which can separate the traversal behavior of the data structure of sequence type from the object being traversed, that is, we do not need to care about what the underlying structure of the sequence looks like. As long as you get the object, you can use an iterator to traverse the inside of the object.
The collection object that Iterable implements this interface supports iterations and can be iterated. Realized this can be used with foreach ~
Iterator iterator, an object that provides an iteration mechanism, and exactly how to iterate is specified by the Iterator interface.
Iterator description
Public interface Iterator {/ / before each next, call this method to detect whether the iteration reaches the end point boolean hasNext (); / / returns the current iteration element, and at the same time, moves the iteration cursor back to E next (); / * deletes the element that was recently iterated out. The remove function can be called only after the next has been executed. For example, if you want to delete the first element, you can't call remove () directly but next it first (); calling the remove method without first calling next will throw an exception. This is very similar to ResultSet in MySQL * / default void remove () {throw new UnsupportedOperationException ("remove");} default void forEachRemaining (Consumer)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.