Java iterator iterator

Source: Internet
Author: User

The purpose of the Java Container Class library is "Save Object", one is collection, one is map.

But no matter what kind of container class, there must be some way to insert elements and retrieve them ! For example, you can use Add () to insert elements in a list, and get () to remove elements.

But the container class must specify the exact type, such as ARRAYLIST<INTERGER>, but what if you want to use Map/set? How to improve the code reuse rate?

An iterator is an object whose work is to traverse and select the objects in the sequence.

    1. Using iterator () requires the container to return a iterator. Iterator returns the first element in a sequence.
    2. Use the next () method to get the next element of the sequence.
    3. Use Hasnext () to check if the sequence has elements.
    4. Use Remove () to delete the element that is newly returned by the iterator.

  

1  Public classMyiterator {2      Public Static voidMain (string[] args) {3Arraylist<integer> alist=NewArraylist<>();4Alist.add (2); Alist.add (4); Alist.add (1);5Iterator<integer> it=alist.iterator ();6System.out.println ("size=" +alist.size ());//output ArrayList original size7System.out.println (It.next ());//using iterators to output the first element of the list8It.remove ();//Delete9System.out.println ("size=" +alist.size ());//output after delete list sizeTen display (it); One         /* A * Change to LinkedList -          * */ - System.out.println (); theLinkedlist<integer> ltd=NewLinkedlist<>(); -Ltd.add (Ltd.add); Ltd.add (33); - display (Ltd.iterator ()); -          +     } -      Public Static voidDisplay (iterator<integer>its ) { +          while(Its.hasnext ()) { A             intn=Its.next (); atSystem.out.print (n+ ""); -         } -     } -}

Output

size=32Size

  such as the display () method, do not need to consider the specific type of container, whether it is LinkedList or ArrayList

The display () method does not contain any type information about the sequence it traverses

Java iterator iterator

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.