Java----data structures and algorithms----The traversal of collection elements: Iterators-------->javaapi:java.util.iterator+java.util.listiterator

Source: Internet
Author: User

Overview:
      • Iterators are used for traversal of collection elements
      • There are two types of iterators, iterator and Listiterator, respectively.
        • Iterator can be used for traversal of any type of collection
        • Listiterator can only be used for traversal of list collections
        • The Listiterator interface inherits the iterator interface, so the former has all the member functions defined by the latter, and Listiterator also adds some action functions with the list collection attribute, such as accessing collection elements by index, replacing/adding collection elements, etc.
Java.util.Iterator
    • /** * @authorChen * @date 20170310 * @description This program describes how to use the Java.util.Iterator interface * Java.util.Iterator interface * Overview: * 1) iterator dedicated for collection element traversal * 2) Since iterator is just an interface, the member functions listed below are * constructors with no method body: * Itera Tor is an interface, so there is no constructor * member function: * "1" boolean hasnext ();//See if there is a next element in the collection, and if so, return True * "2" E next (); Returns the next element in the collection * "3" void remove ();//delete the element pointed to by this iteration in the collection **/ Packagejavaapi_datastructure;ImportJava.util.Iterator; Public classJavaapi_iterator { Public Static voidMain (string[] args) {Iterator it; }}
Java.util.ListIterator
    • /** * @author Chen * @date 20170310 * @description This program describes the use of Listiterator interface * Java.util.ListIterator interface * Overview: * 1) List The iterator interface inherits the iterator interface, so it has all the member methods defined in the iterator interface * 2) iterator can be used for traversal of all types of collections, but listiterator can only be used for a collection of lists and their implementation class types         Calendar * 3) Since listiterator is an interface, all of the functions listed below are * constructors with no method body: * Because Listiterator is an interface, it does not have constructors * member functions: * "Overview" since Listiterator inherits the iterator interface, it has all of the member methods defined in the iterator interface, * in the functions listed below, with "Inherit from I Terator ", all means that the function is inherited from its parent interface iterator *" Inherits from Iterator Interface "" 1 "boolean hasnext ();//If the list collection has the next element, it returns True *" Inherit from Iterator interface "" 2 "E next ();//Returns the next element of the collection *" 3 "Boolean hasprevious ();//Returns True if there are elements in front of the list. * "4" E previous ();//Combined with the "3" function, used for the reverse traversal of the list collection, this function is used to return the value of the collection element in the reverse pass-through time * "5" int Next index ();//Returns the indexes of the next element with a value range of 1~list.size () * "6" int previousindex ();//Returns the index of the previous element with a value range of -1~list.si Ze ()-2 * "Inherit from Iterator Interface" "7" void remove ();//Delete SetFit element * "8" void set (e e);//replace element in collection * "9" void Add (e e);//At the previous position of the current index or the current                                    Adds a new element to the collection at the back of the index position. *//whether or not to add new elements before or after the current index position depends on whether you are traversing sequentially or in reverse order * If it is {@link#next}, insert at the previous position of the current position *//if it is {@link#previous} to insert at the next position in the current position*/ Packagejavaapi_datastructure; Public classJavaapi_listiterator {}

        

    

Java----data structures and algorithms----The traversal of collection elements: Iterators-------->javaapi:java.util.iterator+java.util.listiterator

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.