LoslegenKostenlos loslegen

Working with Queue

Queues collect and returns objects in the order they were added. Queues also have a capacity. Here you create a waiting list Queue (specifically from an ArrayBlockingQueue) and add names to it. You see what happens when trying to add more names than the capacity allows.

Diese Übung ist Teil des Kurses

Data Types and Exceptions in Java

Kurs anzeigen

Anleitung zur Übung

  • Import ArrayBlockingQueue for use in the application.
  • Construct a new ArrayBlockingQueue of Strings with a capacity of 3 and set the waitList variable to it.
  • Add a new name, "Tarah", to waitList.
  • Remove the first name on the waitList.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

// Import ArrayBlockingQueue
____ ____.____.____.____;

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);
	}
}
Code bearbeiten und ausführen