黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 Java遍歷List的方式

Java遍歷List的方式

更新時間:2022-04-22 10:23:16 來源:動力節點 瀏覽1916次

Java遍歷List的方式有哪些?動力節點小編來告訴大家。

有 7 種方法可以遍歷 List。

簡單的 For 循環語句

增強的 For 循環

迭代器

列表迭代器

While 循環

Iterable.forEach() 工具

Stream.forEach() 工具

Java 示例:

您需要 JDK 13 來運行下面的程序,point-5如上使用stream()util。

void java.util.stream.Stream.forEach(Consumer action)對此流的每個元素執行一個操作。

package crunchify.com.tutorials; 
import java.util.*;
/**
 * @author Crunchify.com
 * How to iterate through Java List? Seven (7) ways to Iterate Through Loop in Java.
 * 1. Simple For loop
 * 2. Enhanced For loop
 * 3. Iterator
 * 4. ListIterator
 * 5. While loop
 * 6. Iterable.forEach() util
 * 7. Stream.forEach() util
 */ 
public class CrunchifyIterateThroughList { 
    public static void main(String[] argv) { 
        // create list
        List<String> crunchifyList = new ArrayList<String>();
        // add 4 different values to list
        crunchifyList.add("Facebook");
        crunchifyList.add("Paypal");
        crunchifyList.add("Google");
        crunchifyList.add("Yahoo"); 
        // Other way to define list is - we will not use this list :)
        List<String> crunchifyListNew = Arrays.asList("Facebook", "Paypal", "Google", "Yahoo"); 
        // Simple For loop
        System.out.println("==============> 1. Simple For loop Example.");
        for (int i = 0; i < crunchifyList.size(); i++) {
            System.out.println(crunchifyList.get(i));
        } 
        // New Enhanced For loop
        System.out.println("\n==============> 2. New Enhanced For loop Example..");
        for (String temp : crunchifyList) {
            System.out.println(temp);
        } 
        // Iterator - Returns an iterator over the elements in this list in proper sequence.
        System.out.println("\n==============> 3. Iterator Example...");
        Iterator<String> crunchifyIterator = crunchifyList.iterator();
        while (crunchifyIterator.hasNext()) {
            System.out.println(crunchifyIterator.next());
        } 
        // ListIterator - traverse a list of elements in either forward or backward order
        // An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration,
        // and obtain the iterator's current position in the list.
        System.out.println("\n==============> 4. ListIterator Example...");
        ListIterator<String> crunchifyListIterator = crunchifyList.listIterator();
        while (crunchifyListIterator.hasNext()) {
            System.out.println(crunchifyListIterator.next());
        } 
        // while loop
        System.out.println("\n==============> 5. While Loop Example....");
        int i = 0;
        while (i < crunchifyList.size()) {
            System.out.println(crunchifyList.get(i));
            i++;
        } 
        // Iterable.forEach() util: Returns a sequential Stream with this collection as its source
        System.out.println("\n==============> 6. Iterable.forEach() Example....");
        crunchifyList.forEach((temp) -> {
            System.out.println(temp);
        }); 
        // collection Stream.forEach() util: Returns a sequential Stream with this collection as its source
        System.out.println("\n==============> 7. Stream.forEach() Example....");
        crunchifyList.stream().forEach((crunchifyTemp) -> System.out.println(crunchifyTemp));
    }
}

輸出:

==============> 1. Simple For loop Example.
Facebook
Paypal
Google
Yahoo
==============> 2. New Enhanced For loop Example..
Facebook
Paypal
Google
Yahoo
==============> 3. Iterator Example...
Facebook
Paypal
Google
Yahoo 
==============> 4. ListIterator Example...
Facebook
Paypal
Google
Yahoo 
==============> 5. While Loop Example....
Facebook
Paypal
Google
Yahoo 
==============> 6. Iterable.forEach() Example....
Facebook
Paypal
Google
Yahoo 
==============> 7. Stream.forEach() Example....
Facebook
Paypal
Google
Yahoo
Process finished with exit code 0

以上就是關于“Java遍歷List的方式”介紹,大家如果想了解更多相關知識,不妨來關注一下動力節點的Java在線學習,里面的課程內容從入門到精通,通俗易懂,適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助哦。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 免费永久在线观看污污的网站 | 黄色免费影视 | 欧美特黄aaaaaa| 欧美性f| 一级毛片www | 韩国在线伦理片 | 亚洲福利精品一区二区三区 | 日韩高清在线免费看 | 亚州中文 | 色播视频在线观看 | 三级经典欧美激情 | 99ri在线| 日韩系列在线 | 国产伦精一区二区三区视频 | 国产伦理在线播放 | 日韩成人精品日本亚洲 | 一级片+国产 | 精品国产一区二区三区成人 | 久久午夜宅男免费网站 | 狠狠色综合久久久久尤物 | 亚洲欧美日韩在线不卡中文 | 天天爱天天做天天爽天天躁 | 日韩美女拍拍免费视频网站 | 欧美极品另类xxx | 久久欧洲视频 | 久久99精品久久久久久欧洲站 | 97精品伊人久久久大香线焦 | 2021天堂在线亚洲精品专区 | 九九影院韩国理伦片 | 成 人 黄 色 视频播放16 | 青草青青在线视频 | 最近免费中文完整视频观看 | 乱系列中文字幕在线视频 | 黄色一级片免费在线观看 | 免费在线精品视频 | 久久精品视频日本 | 亚洲伦理一区二区 | 在线免费观看亚洲视频 | 一级毛片成人午夜 | 日日操网站 | 亚洲首页成人有声小说网 |