Java中四種遍曆List的方法

來源:互聯網
上載者:User

標籤:style   static   ack   iterator   span   author   public   tree   new   

 1 package com.ietree.basic.collection.loop; 2  3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6  7 /** 8  * List遍曆 9  * 10  * @author Dylan11  */12 public class ListLoop {13 14     public static void main(String[] args) {15 16         // 初始化一個長度為10的ArrayList17         List<Integer> list = new ArrayList<Integer>();18         for (int i = 0; i < 10; i++) {19             list.add(i);20         }21 22         // 迴圈List的幾種方法:23         // 方式一:24         Iterator it1 = list.iterator();25         while (it1.hasNext()) {26             System.out.println(it1.next());27         }28 29         // 方式二:30         for (Iterator it2 = list.iterator(); it2.hasNext();) {31             System.out.println(it2.next());32         }33 34         // 方式三:35         for (Integer temp : list) {36             System.out.println(temp);37         }38 39         // 方式四:40         for (int i = 0; i < list.size(); i++) {41             System.out.println(list.get(i));42         }43     }44 45 }

 

Java中四種遍曆List的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.