当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ArrayDeque forEach()用法及代码示例


ArrayDeque的forEach()方法是从接口java.lang.Iterable继承的。如果方法指定了操作顺序,则该操作将按照迭代顺序执行。方法遍历ArrayDeque的Iterable的每个元素,直到该方法处理完所有元素或发生异常为止。操作抛出的异常将传递给调用方。

用法:

public void forEach(Consumer<? super E> action)

参数:此方法采用参数名为action,该操作表示要对每个元素执行的操作。


返回值:此方法返回Nothing。

异常:如果指定的操作为null,则此方法抛出NullPointerException。

以下示例程序旨在说明ArrayDeque的forEach()方法:

示例1:在ArrayDeque上演示forEach()方法,该方法包含一个字符串值队列。

// Java Program Demonstrate forEach() 
// method of ArrayDeque 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ArrayDeque 
        // which contains string values 
        ArrayDeque<String> cities = new ArrayDeque<String>(); 
  
        // Add Strings to list 
        cities.add("Kolkata"); 
        cities.add("Delhi"); 
        cities.add("Bombay"); 
        cities.add("Pune"); 
  
        // forEach method of ArrayDeque and 
        // print city names 
        cities.forEach((n) -> System.out.println(n)); 
    } 
}
输出:
Kolkata
Delhi
Bombay
Pune

示例2:在ArrayDeque上演示forEach()方法,该方法包含对象队列。

// Java Program Demonstrate forEach() 
// method of ArrayDeque 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ArrayDeque which going to 
        // contains a list of Objects 
        ArrayDeque<batch> list = new ArrayDeque<batch>(); 
  
        // Add Objects to list 
        list.add(new batch("CSE", 67)); 
        list.add(new batch("ECE", 57)); 
        list.add(new batch("IT", 90)); 
        list.add(new batch("ME", 78)); 
  
        // print result 
        System.out.println("list of Objects:"); 
  
        // forEach method of ArrayDeque and 
        // print student names 
        list.forEach((n) -> print(n)); 
    } 
  
    // printing details of object 
    public static void print(batch n) 
    { 
        System.out.println("*******************************"); 
        System.out.println("Batch Name is " + n.name); 
        System.out.println("No of Students are " + n.noOfStudents); 
    } 
} 
  
// create a class 
class batch { 
  
    String name; 
    int noOfStudents; 
  
    batch(String name, int noOfStudents) 
    { 
        this.name = name; 
        this.noOfStudents = noOfStudents; 
    } 
}
输出:
list of Objects:
*******************************
Batch Name is CSE
No of Students are 67
*******************************
Batch Name is ECE
No of Students are 57
*******************************
Batch Name is IT
No of Students are 90
*******************************
Batch Name is ME
No of Students are 78

示例3:在ArrayDeque上演示forEach()方法的NullPointerException。

// Java Program Demonstrate forEach() 
// method of ArrayDeque 
  
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ArrayDeque 
        // which contains string values 
        ArrayDeque<String> cities = new ArrayDeque<String>(); 
  
        // Add Strings to list 
        cities.add("Kolkata"); 
        cities.add("Delhi"); 
        cities.add("Bombay"); 
        cities.add("Pune"); 
  
        try { 
            // forEach method of ArrayDeque and 
            // print city names 
            cities.forEach(null); 
        } 
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
输出:
Exception: java.lang.NullPointerException

参考:
https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#forEach(java.util.function.Consumer)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ArrayDeque forEach() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。