- 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 PriorityQueue clear() 方法
描述
Java PriorityQueue clear() 方法会移除此队列中的所有元素。当我们想要重用队列或重置队列时,此方法非常有用。
声明
以下是 java.util.PriorityQueue.clear() 方法的声明
public void clear()
参数
无
返回值
此方法不返回任何值。
异常
无
清空整数 PriorityQueue 的示例
以下示例展示了 Java PriorityQueue clear() 方法的使用。在此示例中,我们使用的是整数。第一步,我们填充 arraydeque 对象并打印它。然后,我们打印 arraydeque 对象的大小并执行清空操作。清空后,我们打印 arraydeque 对象的大小,现在为 0。
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<Integer> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add(20); queue.add(30); queue.add(10); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // finding size of this queue int retval = queue.size(); System.out.println("PriorityQueue consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); queue.clear(); retval = queue.size(); System.out.println("Now, queue consists of "+ retval +" elements"); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
PriorityQueue = [10, 30, 20] PriorityQueue consists of 3 elements Performing clear operation !! Now, queue consists of 0 elements
清空字符串 PriorityQueue 的示例
以下示例展示了 Java PriorityQueue clear() 方法的使用。在此示例中,我们使用的是字符串。第一步,我们填充 arraydeque 对象并打印它。然后,我们打印 arraydeque 对象的大小并执行清空操作。清空后,我们打印 arraydeque 对象的大小,现在为 0。
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<String> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add("A"); queue.add("B"); queue.add("C"); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // finding size of this queue int retval = queue.size(); System.out.println("PriorityQueue consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); queue.clear(); retval = queue.size(); System.out.println("Now, queue consists of "+ retval +" elements"); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
PriorityQueue = [A, B, C] PriorityQueue consists of 3 elements Performing clear operation !! Now, queue consists of 0 elements
清空对象 PriorityQueue 的示例
以下示例展示了 Java PriorityQueue clear() 方法的使用。在此示例中,我们使用的是 Student 对象。第一步,我们填充 arraydeque 对象并打印它。然后,我们打印 arraydeque 对象的大小并执行清空操作。清空后,我们打印 arraydeque 对象的大小,现在为 0。
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<Student> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add(new Student(1, "Julie")); queue.add(new Student(2, "Robert")); queue.add(new Student(3, "Adam")); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // finding size of this queue int retval = queue.size(); System.out.println("PriorityQueue consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); queue.clear(); retval = queue.size(); System.out.println("Now, queue consists of "+ retval +" elements"); } } class Student implements Comparable<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); } @Override public int compareTo(Student student) { return this.rollNo - student.rollNo; } }
输出
让我们编译并运行上述程序,这将产生以下结果:
PriorityQueue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] PriorityQueue consists of 3 elements Performing clear operation !! Now, queue consists of 0 elements
java_util_priorityqueue.htm
广告