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


Java ArrayDeque contains()用法及代碼示例


Java中的Java.util.ArrayDeque.contains()方法用於檢查或驗證Deque中是否存在特定元素。

用法:

Array_Deque.contains(Object element)

參數:參數element 的類型跟ArrayDeque中元素相同。用於檢查雙端隊列中是否存在此元素。


返回值:如果雙端隊列中存在元素,則該方法返回True,否則返回False。

下麵的程序演示了Java.util.ArrayDeque.contains()方法:
示例1:

// Java code to illustrate contains() 
import java.util.*; 
  
public class ArrayDequeDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty ArrayDeque 
        Deque<String> de_que = new ArrayDeque<String>(); 
  
        // Use add() method to add elements into the Queue 
        de_que.add("Welcome"); 
        de_que.add("To"); 
        de_que.add("Geeks"); 
        de_que.add("4"); 
        de_que.add("Geeks"); 
  
        // Displaying the ArrayDeque 
        System.out.println("ArrayDeque: " + de_que); 
  
        // Check for "Geeks" in the deque 
        System.out.println("Does the deque contains 'Geeks'? "
                           + de_que.contains("Geeks")); 
  
        // Check for "4" in the deque 
        System.out.println("Does the deque contains '4'? "
                           + de_que.contains("4")); 
  
        // Check if the deque contains "No" 
        System.out.println("Does the deque contains 'No'? "
                           + de_que.contains("No")); 
    } 
}
輸出:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
Does the deque contains 'Geeks'? true
Does the deque contains '4'? true
Does the deque contains 'No'? false

示例2:

// Java code to illustrate contains() 
import java.util.*; 
  
public class ArrayDequeDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty ArrayDeque 
        Deque<Integer> de_que = new ArrayDeque<Integer>(); 
  
        // Use add() method to add elements into the Queue 
        de_que.add(10); 
        de_que.add(15); 
        de_que.add(30); 
        de_que.add(20); 
        de_que.add(5); 
  
        // Displaying the ArrayDeque 
        System.out.println("ArrayDeque: " + de_que); 
  
        // Check for '15' in the Deque 
        System.out.println("Does the Deque contains '15'? "
                           + de_que.contains(15)); 
  
        // Check for '2' in the Deque 
        System.out.println("Does the Deque contains '2'? "
                           + de_que.contains(2)); 
  
        // Check if the Deque contains '10' 
        System.out.println("Does the Deque contains '10'? "
                           + de_que.contains(10)); 
    } 
}
輸出:
ArrayDeque: [10, 15, 30, 20, 5]
Does the Deque contains '15'? true
Does the Deque contains '2'? false
Does the Deque contains '10'? true


相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 ArrayDeque contains() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。