Java Iterator (Iterator) general usage, javaiterator

Source: Internet
Author: User

Java Iterator (Iterator) general usage (conversion), javaiterator
Iterator is a design pattern. It is an object that can traverse and select objects in a sequence. Developers do not need to understand the underlying structure of the sequence. An iterator is usually called a "lightweight" object because it is easy to create. The Iterator function in Java is relatively simple and can only be moved one way: (1) the method iterator () requires the container to return an Iterator. When the next () method of Iterator is called for the first time, it returns the first element of the sequence. Note: The iterator () method is a java. lang. Iterable interface inherited by Collection. (2) Use next () to obtain the next element in the sequence. (3) Use hasNext () to check whether there are any elements in the sequence. (4) use remove () to delete the elements returned by the iterator. Iterator is the simplest implementation of the Java Iterator. The ListIterator designed for List has more functions. It can traverse the List in two directions, or insert and delete elements from the List. Iterator application: list l = new ArrayList (); l. add ("aa"); l. add ("bb"); l. add ("cc"); for (Iterator iter = l. iterator (); iter. hasNext ();) {String str = (String) iter. next (); System. out. println (str );}
What is the return value of the container Iterator () in Java?

The returned Iterator is not the first Node. You need to call the next method of Iterator to get the Node through iteration.

Hope to help you.

How to use java iterator

For example, it is easy for the landlord to understand

Import java. util. ArrayList;
Import java. util. Iterator;

Public class Test {
Public static void main (String [] args ){
ArrayList list = new ArrayList ();
List. add (1 );
List. add (2 );
List. add (3 );
List. add (4 );
List. add (5 );
Iterator it = list. iterator (); // General iterator Iterator, put list into it
While (it. hasNext () {// determine whether there is a next element
Integer I = (Integer) it. next (); // force transformation
System. out. println (I); // cyclic output
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.