Java 集合的迭代操作 Iterator

來源:互聯網
上載者:User

標籤:obj   stat   blog   sys   created   util   turn   use   int   

幾種實現 列出如下  

 1 import java.util.*; 2  3 /** 4  * Created by wojia on 2017/6/13. 5  */ 6 public class IteratorDemo { 7         public static void main(String args[]){ 8             List list = new ArrayList(); 9             list.add("A");10             list.add("B");11             list.add("C");12             list.add("D");13             System.out.println(list);14             /*list 集合的迭代操作*/15             //1. for loop16             for(int index = 0 ; index < list.size() ; index++){ //it‘s method which declared as size() , not a variable  size.17                 Object temp = list.get(index);18                 System.out.print(temp);19 20             }21             System.out.println();22 23             //2. for - each loop24             for(Object ele : list){25                 System.out.print(ele);26             }27             System.out.println();28 29             //3. Use iterator30             /**list.iterator will return an iterator Object*/31             Iterator iterator = list.iterator();32             while(iterator.hasNext()){33                 System.out.print(iterator.next());//next()is a method34             }35 36             //4. Use for loop37             /**38              * this method is the best ,better than while loop , because of the gc 39              * */40             for(Iterator it = list.iterator();iterator.hasNext();){41                 System.out.print(it.next());42             }43 44         }45 46     }

  

Java 集合的迭代操作 Iterator

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.