位置:首頁 > Java技術 > Java.util包 > Java.util.ArrayDeque.contains(Object)方法實例

Java.util.ArrayDeque.contains(Object)方法實例

java.util.ArrayDeque.contains(Object) 如果此deque隊列包含指定的元素,則返回true。

聲明

以下是java.util.ArrayDeque.contains()方法的聲明

public boolean contains(Object o) 

參數

  • o -- 要檢查包含在deque隊列的對象。

返回值

如果此deque隊列包含指定的元素此方法返回true,否則返回false。

異常

  • NA

例子

下麵的示例演示java.util.ArrayDeque.contains()方法的用法。

package com.yiibai;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque with an initial capacity
      Deque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(20);
      deque.add(30);
      deque.add(10);
      deque.add(18);        
       
      // let us print all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
       }

      // deque contains element 10
      boolean retval = deque.contains(10); 
	  
      if (retval == true) {
        System.out.println("element 10 is contained in the deque");
       }
      else {
        System.out.println("element 10 is not contained in the deque");
       }
		
      // deque does not contain element 25
      boolean retval2 = deque.contains(25);
	  
      if (retval2 == true) {
        System.out.println("element 25 is contained in the deque");
       }
      else {
        System.out.println("element 25 is not contained in the deque");    
       }
   }
}      

讓我們來編譯和運行上麵的程序,這將產生以下結果:

Number = 20
Number = 30
Number = 10
Number = 18
element 10 is contained in the deque
element 25 is not contained in the deque