- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包其他内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
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
广告