Java ArrayDeque isEmpty() 方法



描述

Java ArrayDeque isEmpty() 方法如果此双端队列不包含任何元素则返回true,否则返回false

声明

以下是java.util.ArrayDeque.isEmpty() 方法的声明

public boolean isEmpty()

参数

返回值

如果此双端队列不包含任何元素,则此方法返回true,否则返回false

异常

检查整数型 ArrayDeque 是否为空的示例

以下示例展示了 Java ArrayDeque isEmpty() 方法的使用。这里我们使用的是整数型 ArrayDeque。首先,我们初始化一个 ArrayDeque 对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查 arraydeque 对象是否为空。

package com.tutorialspoint;

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

public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Integer> deque = new ArrayDeque<>();

      if (deque.isEmpty()) {
         System.out.println("deque is empty");
      } else {
         System.out.println("deque is not empty");
      }

      // printing all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // use add() method to add elements in the deque
      deque.add(25);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      if (deque.isEmpty()) {
         System.out.println("deque is empty");
      } else {
         System.out.println("deque is not empty");
      }

      // printing all the elements available in deque
      System.out.println("ArrayDeque = " + deque);
   }
}

输出

让我们编译并运行以上程序,这将产生以下结果:

deque is empty
ArrayDeque = []
deque is not empty
ArrayDeque = [25, 30, 20, 18]

检查字符串型 ArrayDeque 是否为空的示例

以下示例展示了 Java ArrayDeque isEmpty() 方法与字符串型 ArrayDeque 的用法。首先,我们初始化一个 ArrayDeque 对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查 arraydeque 对象是否为空。

package com.tutorialspoint;

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

public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<String> deque = new ArrayDeque<>();

      if (deque.isEmpty()) {
         System.out.println("deque is empty");
      } else {
         System.out.println("deque is not empty");
      }

      // printing all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // use add() method to add elements in the deque
      deque.add("A");
      deque.add("B");
      deque.add("C");
      deque.add("D");        

      if (deque.isEmpty()) {
         System.out.println("deque is empty");
      } else {
         System.out.println("deque is not empty");
      }

      // printing all the elements available in deque
      System.out.println("ArrayDeque = " + deque);
   }
}

输出

让我们编译并运行以上程序,这将产生以下结果:

deque is empty
ArrayDeque = []
deque is not empty
ArrayDeque = [A, B, C, D]

检查对象型 ArrayDeque 是否为空的示例

以下示例展示了 Java ArrayDeque isEmpty() 方法与 Student 对象型 ArrayDeque 的用法。首先,我们初始化一个 ArrayDeque 对象,然后检查它是否为空。然后我们将添加一些元素,然后再次检查 arraydeque 对象是否为空。

package com.tutorialspoint;

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

public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Student> deque = new ArrayDeque<>();

      if (deque.isEmpty()) {
         System.out.println("deque is empty");
      } else {
         System.out.println("deque is not empty");
      }

      // printing all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // use add() method to add elements in the deque
      deque.add(new Student(1, "Julie"));
      deque.add(new Student(2, "Robert"));
      deque.add(new Student(3, "Adam"));       

      if (deque.isEmpty()) {
         System.out.println("deque is empty");
      } else {
         System.out.println("deque is not empty");
      }

      // printing all the elements available in deque
      System.out.println("ArrayDeque = " + deque);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

输出

让我们编译并运行以上程序,这将产生以下结果:

deque is empty
ArrayDeque = []
deque is not empty
ArrayDeque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_arraydeque.htm
广告