CommencerCommencez gratuitement

Travailler avec Queue

Queues collect and return objects in the order they were added. Queues also have a capacity. Here, you create a waiting listQueue(specifically from anArrayBlockingQueue`) and add names to it. You see what happens when trying to add more names than the capacity allows.

Cet exercice fait partie du cours

<cours>Types de données et exceptions en Java</cours>
Voir le cours

Instructions de l’exercice

  • Importez ArrayBlockingQueue pour l’utiliser dans l’application.
  • Créez une nouvelle ArrayBlockingQueue de String avec une capacité de 3 et affectez-la à la variable waitList.
  • Ajoutez un nouveau nom, "Tarah", à waitList.
  • Supprimez le premier nom de waitList.

Exercice interactif pratique

Essayez cet exercice en complétant ce code d’exemple.

// 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);
	}
}
Modifier et exécuter le code