Java遍历List的方式_极悦注册
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java遍历List的方式

Java遍历List的方式

更新时间:2022-04-22 10:23:16 来源:极悦 浏览1485次

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极悦在线学习,里面的课程内容从入门到精通,通俗易懂,适合没有基础的小伙伴学习,希望对大家能够有所帮助哦。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>