6 ways to traverse the collection collection:

Source: Internet
Author: User

The following code demonstrates the 6 ways to traverse the collection collection, noting that the collection collection's traversal is much more than the enhanced for loop, and the iterator two.

The code is as follows:

1  PackageCom.qls.traverse;2 3 Importjava.util.ArrayList;4 Importjava.util.Arrays;5 Importjava.util.Collections;6 Importjava.util.Enumeration;7 ImportJava.util.Iterator;8 Importjava.util.LinkedList;9 Importjava.util.List;Ten ImportJava.util.Stack; One  A /** - * Here are a few ways to traverse collection, with the list interface as an example: -  * @authorQuinlinson the  * -  */ -  Public classListtest { -  +      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub +String[] s= "Sixi is one of the most beautiful villages". Split (""); Alist<string> list =Arrays.aslist (s); at         /** - * The first method is enhanced for loop. (The reason why the list can be enhanced for loops here is because it implements the Iterable interface) -          */ -          for(String str:list) { -System.out.print (str+ ""); -         } in System.out.println (); -System.out.println ("************"); to         /** + * The second method uses iterator -          */ theIterator<string> it =list.iterator (); *          while(It.hasnext ()) { $String next =It.next ();Panax NotoginsengSystem.out.print (next+ ""); -         } the System.out.println (); +System.out.println ("************"); A         /** the * The third method is mainly for LinkedList. Because LinkedList has both stack characteristics and queues (queue) + * features. So iterate through the elements in the LinkedList. Depending on the stack and queue, the related traversal can be performed.  - * The method of traversal is as follows: $          */ $         //Using LinkedList as a stack -Linkedlist<string> list2=NewLinkedlist<> (list);//creates a linkelist that contains all the elements in the list.  -          while(!List2.isempty ()) { theSystem.out.print (List2.removefirst () + ""); -         }Wuyi System.out.println (); theSystem.out.println ("************"); -         /** Wu * Using LinkedList as a queue -          */ AboutLinkedlist<string> list3=NewLinkedlist<>(list); $          while(List3.peek ()! =NULL){ -System.out.print (List3.poll () + ""); -         } - System.out.println (); ASystem.out.println ("************"); +         /** the * Fourth method to traverse all collection as enumeration - * Collections.enumeration (c) $          */ theArraylist<string> list4=NewArraylist<>(list); theEnumeration<string> e =collections.enumeration (LIST4); the          while(E.hasmoreelements ()) { theSystem.out.print (e.nextelement () + ""); -         } in         /**Fifth Method the * Of course there are other methods such as: the          */ About System.out.println (); theSystem.out.println ("************"); the          for(intI=0;i<list4.size (); i++){ theSystem.out.print (List4.get (i) + ""); +         } - System.out.println (); theSystem.out.println ("************");Bayi         /**The Sixth method: the * again as: the          */ -          while(!List4.isempty ()) { -             intIndex=0; theSystem.out.print (List4.remove (index++) + ""); the         } the         /** the * Note: ArrayList LinkedList is most commonly used in all implementation classes in the list interface . - * Arralist is faster than LinkedList, generally the case of ArrayList is more than LinkedList.  the * There is a serialversionuid in the ArrayList source code, this number is guaranteed, the * Write File (Objectoutputstream.writeobject (Object)) the * Read the file (Objectinputstream.readobject ()) can proceed smoothly,94 * and indicate this number, you can maintain the compatibility of each version. facilitates file transfer.  the          */ the          the     }98  About}/*Output: - Sixi is one of the most beautiful villages in China101 ************102 Sixi is one of the most beautiful villages in China103 ************104 Sixi is one of the most beautiful villages in China the ************106 Sixi is one of the most beautiful villages in China107 ************108 Sixi is one of the most beautiful villages in China109 ************ the Sixi is one of the most beautiful villages in China111 ************ the Sixi is one of the most beautiful villages in China*///:~

6 ways to traverse the collection collection:

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.