IT grow. 2018. 7. 9. 15:38
반응형

QUESTION 8 


Given that the elements of a PriorityQueue are ordered according to natural ordering, and: 

 

import java.util.*; 

public class GetInLine 

{

     public static void main(String[] args) {

         PriorityQueue<String> pq = new PriorityQueue<String>();

         pq.add("banana");

         pq.add("pear");

         pq.add("apple");

         System.out.println(pq.poll() + " " + pq.peek());

     }

 } 

 

What is the result? 

 

A. apple pear 

B. banana pear 

C. apple apple 

D. apple banana 

E. banana banana 


Solution : D


plus imformation : 


poll 메소드 : Queue 에서 데이터를 꺼내온다 . 만일 queue가 비어있다면 null을 반환

peek 메소드 : 큐의 맨 아래 있는 객체를 반환한다 . 이 때 객체를 큐에서 제거하진 않는다.

 

반응형