Three ways to traverse the list collection in Java (full network details)

Source: Internet
Author: User

  List collection In the Java daily development is essential, as long as the use of a variety of methods can greatly improve the efficiency of our development, the appropriate use of various methods will make our development more efficient.

I have summed up the traversal of the three list collections, as described below hit.

First, create an entity class for use below.

 Public classnews{Private intID; PrivateString title; PrivateString author;  PublicNews (intID, string title, string author) {        Super();  This. ID =ID;  This. title =title;  This. Author =author; }         Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetTitle () {returntitle; }     Public voidSettitle (String title) { This. title =title; }     PublicString Getauthor () {returnauthor; }     Public voidSetauthor (String author) { This. Author =author; }}

The first Kind, the most basic traversal method: For loop, specify subscript length, use the size () method of the list collection for A For loop traversal

  

Import java.util.ArrayList;

public class Demo01 {

public static void Main (string[] args) {

arraylist<news> list = new arraylist<news> ();
    
List.add (New News (1, "List1", "a"));
List.add (New News (2, "List2", "B"));
List.add (New News (3, "List3", "C"));
List.add (New News (4, "List4", "D"));
 for (int i = 0; i < list.size (); i++) {            = (News) list.get (i);            System.out.println (S.getid ()+ "  " +s.gettitle () + ""  +S.getauthor ()); }
}
}

The second kind, more concise traversal: Use foreach to traverse the list, but not to manipulate an element (a method that works as well when iterating through arrays and map collections)

Import java.util.ArrayList;  Public class Demo02 {  publicstaticvoid  main (string[] args) {ArrayListnew Ar Raylist<news>();

List.add (New News (1, "List1", "a"));
List.add (New News (2, "List2", "B"));
List.add (New News (3, "List3", "C"));
List.add (New News (4, "List4", "D"));

     for   (News s:list) {            System.out.println (S.getid ()+ "  " +s.gettitle () + "  " +S.getauthor ()); }}}

third , applicable iterator iterator traversal: automatic traversal directly based on the list collection

  Specific steps:

① uses a list call, iterator () returns an Iterator object;
② uses an Iterator object call, Hasnext () to determine whether there is a next piece of data;
③ is called with an iterator object.

Importjava.util.ArrayList; Public classDemo03 { Public Static voidMain (string[] args) {ArrayList<News> list =NewArraylist<news>(); List.add (NewNews (1, "List1", "a")); List.add (NewNews (2, "List2", "B")); List.add (NewNews (3, "List3", "C")); List.add (NewNews (4, "List4", "D")); Iterator<News> iter =List.iterator ();  while(Iter.hasnext ()) {News s=(News) iter.next (); System.out.println (S.getid ()+ "" "+s.gettitle () +" "+S.getauthor ()); }}}

The above three kinds of traversal methods are not particularly large, can be used when there is no specific requirements. However, according to different special circumstances, we should choose a reasonable way to improve the efficiency of development.

Three ways to traverse the list collection in Java (full network details)

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.