शुरू करेंमुफ़्त में शुरू करें

Queue के साथ काम करना

Queues ऑब्जेक्ट्स को उसी क्रम में इकट्ठा करती और लौटाती हैं, जिस क्रम में वे जोड़े गए थे. Queues की एक capacity भी होती है. यहाँ, आप एक waiting list Queue (खासतौर पर ArrayBlockingQueue से) बनाएँगे और उसमें नाम जोड़ेंगे. आप देखेंगे कि जब capacity से ज़्यादा नाम जोड़ने की कोशिश की जाती है तो क्या होता है.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Java में डेटा टाइप्स और Exceptions

पाठ्यक्रम देखें

अभ्यास निर्देश

  • एप्लिकेशन में उपयोग के लिए ArrayBlockingQueue इम्पोर्ट करें.
  • Strings की एक नई ArrayBlockingQueue capacity 3 के साथ बनाएँ और उसे waitList वैरिएबल में सेट करें.
  • waitList में एक नया नाम "Tarah" जोड़ें.
  • 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);
	}
}
कोड संपादित करें और चलाएँ