大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

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

Java遍歷List的方式

更新時間:2022-04-22 10:23:16 來源:動力節(jié)點 瀏覽1821次

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

有 7 種方法可以遍歷 List。

簡單的 For 循環(huán)語句

增強的 For 循環(huán)

迭代器

列表迭代器

While 循環(huán)

Iterable.forEach() 工具

Stream.forEach() 工具

Java 示例:

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

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

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

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

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 亚洲综合日韩中文字幕v在线 | 亚洲综合色婷婷在线观看 | 特级按摩一级毛片 | 色婷婷在线视频 | 曰本性l交视频 | 天天拍拍夜夜出水 | 国产成人精品久久亚洲高清不卡 | 91色综合综合热五月激情 | 国产高清视频在线播放 | 国产乱码精品一区二区 | 久久精品国1国二国三 | 超级碰碰青草免费视频92 | 日本一区二区在线播放 | 日韩伦理一区 | 在线播放五十路乱中文 | 一级特黄性色生活片 | 俄罗斯三级毛片 | 欧美性理论片在线观看片免费 | 午夜不卡视频 | 日日操干 | 伊人网在线播放 | 污宅男666在线永久免费观看 | 逆天至尊免费看229集 | 亚洲精品人成在线观看 | 亚洲精品在线视频 | 日韩毛片高清在线看 | 国产五月色综合 | 在线视频不卡国产在线视频不卡 | 欧美在线观看a | 欧美日韩一区二区三区自拍 | 黄色小视频在线免费观看 | 国产区在线观看 | 欧美24video| 一级毛片在线播放 | 91在线视频观看 | jizjizjiz亚洲人| 免费区一级欧美毛片 | 久艹在线观看视频 | 亚洲天天操 | 精品免费国产一区二区女 | 中文日本在线 |