- 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
广告