시작하기무료로 시작하기

Queue 다루기

Queues는 객체를 추가한 순서대로 모으고 반환합니다. 또한 용량(capacity)이 있습니다. 여기서는 대기자 명단 Queue(구체적으로는 ArrayBlockingQueue)를 만들고 여기에 이름을 추가해 봅니다. 용량을 초과해 이름을 더 추가하려 할 때 무슨 일이 일어나는지도 확인해 보세요.

이 연습은 강의의 일부입니다

Java의 데이터 타입과 예외

강의 보기

연습 안내

  • 애플리케이션에서 사용할 ArrayBlockingQueue를 임포트하세요.
  • 용량이 3인 StringArrayBlockingQueue를 새로 생성하고 waitList 변수에 할당하세요.
  • 새로운 이름 "Tarah"waitList에 추가하세요.
  • waitList에서 첫 번째 이름을 제거하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

// Import ArrayBlockingQueue
import java.util.____.____;

public class GetInLine {

	public static void main(String[] args) {
    	// Create an ArrayBlockingQueue of up to 3 names using parameterized constructor
		____<____> waitList = new ____<____>(____);
		waitList.offer("Sally");
		waitList.offer("Benny");
        // Add the name "Tarah"
		waitList.____(____);
		System.out.println(waitList);
		waitList.offer("Letty");
		System.out.println(waitList);
        // Remove the first name
		String first = waitList.____();
		System.out.println(first);
		waitList.offer("Letty");
		System.out.println(waitList);
	}
}
코드 편집 및 실행