- 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 offer(E) 方法
描述
Java PriorityQueue offer(E e) 方法將指定的元素 E 插入到此佇列的末尾。它類似於 add() 方法。
宣告
以下是 java.util.PriorityQueue.offer() 方法的宣告
public boolean offer(E e)
引數
e − 要新增到末尾的元素。
返回值
如果元素已新增到此佇列,則此方法返回 true,否則返回 false。
異常
NullPointerException − 如果指定的元素為 null。
將元素新增到整數 PriorityQueue 末尾的示例
以下示例演示了使用整數的 Java PriorityQueue offer(E) 方法。我們使用 add() 方法將一些元素新增到列表中,然後使用 offer() 方法在末尾新增兩個元素。最後,我們列印 PriorityQueue 物件以檢視最終結果。
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(1);
queue.add(2);
queue.add(3);
queue.add(4);
queue.add(5);
queue.add(6);
// the values will be printed in the same order
queue.offer(7);
queue.offer(8);
// let us print all the elements available in queue
System.out.println("PriorityQueue = " + queue);
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
PriorityQueue = [1, 2, 3, 4, 5, 6, 7, 8]
將元素新增到字串 PriorityQueue 末尾的示例
以下示例演示了使用字串的 Java PriorityQueue offer(E) 方法。我們使用 add() 方法將一些元素新增到列表中,然後使用 offer() 方法在末尾新增兩個元素。最後,我們列印 PriorityQueue 物件以檢視最終結果。
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");
queue.add("D");
queue.add("E");
queue.add("F");
// the values will be printed in the same order
queue.offer("G");
queue.offer("H");
// let us print all the elements available in queue
System.out.println("PriorityQueue = " + queue);
}
}
輸出
讓我們編譯並執行上述程式,這將產生以下結果:
PriorityQueue = [A, B, C, D, E, F, G, H]
將元素新增到物件 PriorityQueue 末尾的示例
以下示例演示了使用 Student 物件的 Java PriorityQueue offer(E) 方法。我們使用 add() 方法將一些元素新增到列表中,然後使用 offer() 方法在末尾新增兩個元素。最後,我們列印 PriorityQueue 物件以檢視最終結果。
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"));
// use offer() method to add element at the end of the queue
queue.offer(new Student(4, "Rohan"));
queue.offer(new Student(5, "Sohan"));
// let us print all the elements available in queue
System.out.println("PriorityQueue = " + queue);
}
}
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 ], [ 4, Rohan ], [ 5, Sohan ]]
java_util_priorityqueue.htm
廣告