Iterator interface. Collection output

Source: Internet
Author: User

The following methods are supported in the collection.

Iterator

Listiterator

foreach Output

Emumeration output.

of the collection output Standard Operation

The collection output must form the following ideas: As long as the operation of the assembly output, you must use the iterator interface, which is the most important criteria .

of the Iterator interface Operating principle

Iterator is a specialized iterative output interface, so-called iterative output is to determine the elements of each one, to determine whether there is content, if there is content, then the content output.

for iterator, it is an excuse in itself, so to instantiate it needs to rely on the collection interface to complete .

You can see such a method in the collection interface:

Visible, the iterator interface can be called through the method iterator () inside the collection interface.

Therefore, all classes that inherit from collection have a iterator () method.

Standard method for instantiating iterator interfaces

list<e> all=  new List Subclass <E> ();iterator<e> iter = All.iterator () ;    // instantiating a iterator interface

Iterator's Own method:

Instance:

 Packageclass set;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classtest1{ Public Static voidMain (String args[]) {List<String> all=NewArraylist<string> ();//All.add ("Hello") ; All.add ("_") ; All.add ("World") ; Iterator<String> iter = All.iterator ();//instantiating a iterator interface         while(Iter.hasnext ()) {//determine if there is contentSystem.out.println (Iter.next ());//Output Content        }    }};

Output Result:

Hello_world

These are standard practices for iterator interfaces.

The Remove method is provided in the iterator interface, which is the ability to delete the current object.

 Packageclass set;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classtest1{ Public Static voidMain (String args[]) {List<String> all=NewArraylist<string> ();//All.add ("Hello") ; All.add ("_") ; All.add ("World") ; Iterator<String> iter = All.iterator ();//instantiating a iterator interface         while(Iter.hasnext ()) {//determine if there is contentString str =Iter.next (); if("_". Equals (str))    { iter.remove ();  //  Delete element  }Else{System.out.println (str); //Output Content}} System.out.println ("The collection after deletion:" +All ); }};

Output Result:

HelloWorld the collection after deletion: [Hello, World]

This remove () method is seldom used in practical operations.

The list itself has the Remove () method.

If you use Remove () from list in the process of using the iteration output to perform a delete operation, the code will have a problem.

 Packageclass set;Importjava.util.ArrayList;ImportJava.util.Iterator;Importjava.util.List; Public classtest1{ Public Static voidMain (String args[]) {List<String> all=NewArraylist<string> ();//All.add ("Hello") ; All.add ("_") ; All.add ("World") ; Iterator<String> iter = All.iterator ();//instantiating a iterator interface         while(Iter.hasnext ()) {//determine if there is contentString str =Iter.next (); if("_". Equals (str))    { all.remove (str);  //  Delete element  }Else{System.out.println (str); //Output Content}} System.out.println ("The collection after deletion:" +All ); }};

Output Result:

The collection after hello delete: [Hello, World]

As you can see here, once the list object executes the delete, the loop will stop executing.

When using Iteratror, do not use the Remove () method in the collection class and only use the methods in the iterator interface.

And the iterator is the output from the past, the unidirectional output,

The function is to complete the iterative output operation.

Iterator interface. Collection output

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.