[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.