Different use methods of iterator and listiterator

Source: Internet
Author: User
When using list and set, we often use iterator to traverse the data ). With the downloader, you do not need to interfere with the traversal process. You only need to extract the desired data each time for processing.

However, they are different when used. Both list and set have iterator () to obtain its iterator. For list, you can also get its iterator through listiterator (). The two iterators cannot be used in some cases. The main differences between iterator and listiterator are as follows:

1. listiterator has the add () method, which can be used to add objects to the list. iterator cannot

2. both listiterator and iterator have hasnext () and next () methods, which can implement sequential backward traversal, but listiterator has hasprevious () and previous () methods, which can implement reverse (sequential forward) traverse. Iterator cannot.

3. listiterator can be used to locate the current index location. nextindex () and previousindex () can be used. Iterator does not have this function.

4. All objects can be deleted. However, listiterator can modify objects and set () can be implemented. Iierator can only be traversed and cannot be modified.

Because of these functions of listiterator, you can perform operations on the List data structure such as listing list. In fact, array objects can also be implemented using iterators.

Org. Apache. commons. Collections. iterators. arrayiterator can implement this function. Generally, we can use iterator. If you need to perform record search before and after repetition, you can use listiterator to expand your functions, (a little like the rolling result set in JDBC ).

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.