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

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

Java遍歷List的方式

更新時(shí)間:2022-04-22 10:23:16 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2017次

Java遍歷List的方式有哪些?動(dòng)力節(jié)點(diǎn)小編來(lái)告訴大家。

有 7 種方法可以遍歷 List。

簡(jiǎn)單的 For 循環(huán)語(yǔ)句

增強(qiáng)的 For 循環(huán)

迭代器

列表迭代器

While 循環(huán)

Iterable.forEach() 工具

Stream.forEach() 工具

Java 示例:

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

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

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)知識(shí),不妨來(lái)關(guān)注一下動(dòng)力節(jié)點(diǎn)的Java在線學(xué)習(xí),里面的課程內(nèi)容從入門(mén)到精通,通俗易懂,適合沒(méi)有基礎(chǔ)的小伙伴學(xué)習(xí),希望對(duì)大家能夠有所幫助哦。

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-10-20 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 伊人久久久综在合线久久在播 | 一本大道香蕉久在线不卡视频 | 99re免费视频精品全部 | 特级毛片免费视频 | 久久国产三级 | 日本一级作爱片在线观看 | 国产大学生一级毛片绿象 | 亚洲黄色网址大全 | 青草免费免费观看视频在线 | 欧美激情在线精品三区 | 欧美成人区 | 国产白拍 | 亚洲五月色 | 国产精品精品国产一区二区 | 超97在线观看精品国产 | 国产欧美日韩综合二区三区 | 男女污污在线观看 | 亚洲国产精品久久久久 | 国产片一级aaa毛片视频 | 久久刺激 | 亚洲一级片在线观看 | 性做爰片视频毛片 | 日本一本二本免费播放视频 | 前任4在线观看 | 久久这里有 | 日韩中文字幕a | 欧美激情在线一区二区三区 | 日韩社区| 九九在线 | 欧美一级人与动毛片免费播放 | 欧美久久精品 | 日本在线精品 | 中国特黄特级真人毛片 | 五月天婷婷免费视频观看 | 久久机热这里只有精品无需 | 白云精品视频国产专区 | 亚洲美女视频免费 | 久久99精品一区二区三区 | 欧美亚洲精品小说一区二三区 | 久久青草国产免费观看 | 嘿咻成人免费视频欧美激情 |