當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。