[Java class set] _ listiterator interface notes (instance test)

Source: Internet
Author: User

[Java class set] _ listiterator interface notes

Objectives of this chapter:
Measure the test taker's knowledge about the relationship between listiterator and iterator interfaces.
Measure the test taker's knowledge about the use restrictions of the listiterator interface.

3. Details

Listiterator Interface
The main function of the iterator interface is to provide one-way output from the front to the back. To achieve two-way output from the back to the back, you must use the iterator Sub-interface-listiterator.

The lisiterator interface is defined as follows:
Public interface listiterator <E> extends iterator <E>
Although this interface can provide two-way output, it is a pity that the collection interface does not define operations that can be instantiated for this class. Only the list interface has the instantiation operation of the listiterator interface.

Example: bidirectional output

Import Java. util. arraylist; import Java. util. list; import Java. util. listiterator; public class listiteratordemo01 {public static void main (string [] ARGs) {list <string> All = new arraylist <string> (); All. add ("hello"); All. add ("_"); All. add ("world !!! "); Listiterator <string> iter = All. listiterator (); system. out. println ("output from front to back"); While (ITER. hasnext () {string STR = ITER. next (); system. out. print (STR + ",");} system. out. println ("\ n from back to forward Output"); While (ITER. hasprevious () {string STR = ITER. previous (); system. out. print (STR + ",");}}}

Output:
Output from front to back
Hello, _, world !!! ,
Forward and backward output
World !!! , _, Hello,

At this point, the two-way output operation has been completed.
However, when using this operation, you must note that you must first output data from the front to the back before you can output data from the back to the back.
You can use listiterator to add and replace elements.
Add ()
Set ()

Import Java. util. arraylist; import Java. util. list; import Java. util. listiterator; public class listiteratordemo01 {public static void main (string [] ARGs) {list <string> All = new arraylist <string> (); All. add ("hello"); All. add ("_"); All. add ("world !!! "); Listiterator <string> iter = All. listiterator (); system. out. println ("output from front to back"); While (ITER. hasnext () {string STR = ITER. next (); system. out. print (STR + ","); ITER. set ("Li-" + Str);} system. out. println ("\ n from back to forward Output"); ITER. add ("lxh"); While (ITER. hasprevious () {string STR = ITER. previous (); system. out. print (STR + ",");}}}

Output:

Output from front to back
Hello, _, world !!! ,
Forward and backward output
Lxh, Li-world !!! , Li-_, Li-Hello,

Summary:
1. If you want to use listiteral, you can only use the list interface.
2. If you want to output data from the back to the back, you can only output data from the front to the back first.
3. You only need to know how to add and modify this interface.

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.