SCJP 1.6版考題 193

出自 陳富國維基館
於 2013年3月30日 (六) 00:21 由 Ikk (對話 | 貢獻) 所做的修訂 (新頁面: Given that the elements of a PriorityQueue are ordered according to natural ordering, and: 2. import java.util.*; 3. public class GetInLine{ 4. public static void main(String[] a...)
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
前往: 導覽搜尋
Given that the elements of a PriorityQueue are ordered according to natural ordering, and:
2. import java.util.*;
3. public class GetInLine{
4.   public static void main(String[] args){
5.     PriorityQueue<String> pq = new PriorityQueue<String>();
6.     pq.add("banana");
7.     pq.add("pear");
8.     pq.add("apple");
9.     System.out.println(pq.poll() + " " + pq.peek());
10.   }
11. }


What is the result?
 A. apple pear
 B. banana pear
 C. apple apple
 D. apple banana
 E. banana banana

解答


Ans: D

解說:

 poll  Retrieves and removes the head of this queue, or returns null if this queue is empty.
 peek Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
 PriorityQueue會讓加入的元素以字母順序排列
 (HEAD) apple banana pear
 poll取出apple印出,並將apple從queue移走,peed取出banana,不移出banana