इस ट्यूटोरियल में, हम ArrayDeque वर्ग और उसके तरीकों के बारे में उदाहरणों की मदद से जानेंगे। इसके अलावा, हम एक स्टैक को लागू करने के लिए एरे डीक का उपयोग करना सीखेंगे।
जावा में, हम ArrayDeque
सरणियों का उपयोग करके कतार और डेटा संरचनाओं को लागू करने के लिए कक्षा का उपयोग कर सकते हैं ।
ArrayDeque द्वारा लागू इंटरफेस
ArrayDeque
वर्ग इन दो इंटरफेस लागू करता है:
- जावा कतार इंटरफ़ेस
- जावा Deque इंटरफ़ेस
ArrayDeque बनाना
एक सरणी विचलन बनाने के लिए, हमें java.util.ArrayDeque
पैकेज को आयात करना होगा ।
यहाँ हम जावा में एक ऐरे डीके बना सकते हैं:
ArrayDeque animal = new ArrayDeque();
यहाँ, Type, array deque के प्रकार को इंगित करता है। उदाहरण के लिए,
// Creating String type ArrayDeque ArrayDeque animals = new ArrayDeque(); // Creating Integer type ArrayDeque ArrayDeque age = new ArrayDeque();
ArrayDeque के तरीके
ArrayDeque
कक्षा में सभी तरीकों वर्तमान कार्यान्वयन प्रदान करता है Queue
और Deque
इंटरफेस।
तत्वों को Deque में डालें
1. Add (), addFirst () और addLast () का उपयोग करके तत्व जोड़ें
add()
- सरणी के अंत में निर्दिष्ट तत्व सम्मिलित करता हैaddFirst()
- ऐरे डी की शुरुआत में निर्दिष्ट तत्व सम्मिलित करता हैaddLast()
- सरणी के अंत में निर्दिष्ट निर्दिष्ट सम्मिलित करता है (समतुल्यadd()
)
नोट: यदि सरणी विचलन पूर्ण है, तो ये सभी विधियाँ add()
, addFirst()
और addLast()
फेंकता है IllegalStateException
।
उदाहरण के लिए,
import java.util.ArrayDeque; class Main ( public static void main(String() args) ( ArrayDeque animals= new ArrayDeque(); // Using add() animals.add("Dog"); // Using addFirst() animals.addFirst("Cat"); // Using addLast() animals.addLast("Horse"); System.out.println("ArrayDeque: " + animals); ) )
आउटपुट
ArrayDeque: (बिल्ली, कुत्ता, घोड़ा)
2. प्रस्ताव (), ऑफ़रफ़र्स्ट () और ऑफ़रलैस्ट () का उपयोग करके तत्व सम्मिलित करें
offer()
- सरणी के अंत में निर्दिष्ट तत्व सम्मिलित करता हैofferFirst()
- ऐरे डी की शुरुआत में निर्दिष्ट तत्व सम्मिलित करता हैofferLast()
- सरणी के अंत में निर्दिष्ट तत्व सम्मिलित करता है
ध्यान दें: offer()
, offerFirst()
और offerLast()
रिटर्न true
अगर तत्व सफलतापूर्वक डाला जाता है; यदि सरणी विराम भरा हुआ है, तो ये विधियाँ वापस आती हैं false
।
उदाहरण के लिए,
import java.util.ArrayDeque; class Main ( public static void main(String() args) ( ArrayDeque animals= new ArrayDeque(); // Using offer() animals.offer("Dog"); // Using offerFirst() animals.offerFirst("Cat"); // Using offerLast() animals.offerLast("Horse"); System.out.println("ArrayDeque: " + animals); ) )
आउटपुट
ArrayDeque: (बिल्ली, कुत्ता, घोड़ा)
नोट: यदि सरणी विराम भरा हुआ है
add()
विधि एक अपवाद फेंक होगाoffer()
विधि रिटर्नfalse
ऐरेडेक तत्वों तक पहुंचें
1. GetFirst () और getLast () का उपयोग करके एक्सेस तत्व
getFirst()
- ऐरे डीके का पहला तत्व देता हैgetLast()
- सरणी विलेख का अंतिम तत्व देता है
नोट: यदि सरणी विलेख खाली है, getFirst()
और getLast()
फेंकता है NoSuchElementException
।
उदाहरण के लिए,
import java.util.ArrayDeque; class Main ( public static void main(String() args) ( ArrayDeque animals= new ArrayDeque(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayDeque: " + animals); // Get the first element String firstElement = animals.getFirst(); System.out.println("First Element: " + firstElement); // Get the last element String lastElement = animals.getLast(); System.out.println("Last Element: " + lastElement); ) )
आउटपुट
ArrayDeque: (कुत्ता, बिल्ली, घोड़ा) पहला तत्व: कुत्ता अंतिम तत्व: घोड़ा
2. एक्सेस तत्वों का उपयोग करके झांकना (), पीकफर्स्ट () और पीकलैस्ट () विधि
peek()
- ऐरे डीके का पहला तत्व देता हैpeekFirst()
- returns the first element of the array deque (equivalent topeek()
)peekLast()
- returns the last element of the array deque
For example,
import java.util.ArrayDeque; class Main ( public static void main(String() args) ( ArrayDeque animals= new ArrayDeque(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayDeque: " + animals); // Using peek() String element = animals.peek(); System.out.println("Head Element: " + element); // Using peekFirst() String firstElement = animals.peekFirst(); System.out.println("First Element: " + firstElement); // Using peekLast String lastElement = animals.peekLast(); System.out.println("Last Element: " + lastElement); ) )
Output
ArrayDeque: (Dog, Cat, Horse) Head Element: Dog First Element: Dog Last Element: Horse
Note: If the array deque is empty, peek()
, peekFirst()
and getLast()
throws NoSuchElementException
.
Remove ArrayDeque Elements
1. Remove elements using the remove(), removeFirst(), removeLast() method
remove()
- returns and removes an element from the first element of the array dequeremove(element)
- returns and removes the specified element from the head of the array dequeremoveFirst()
- returns and removes the first element from the array deque (equivalent toremove()
)removeLast()
- returns and removes the last element from the array deque
Note: If the array deque is empty, remove()
, removeFirst()
and removeLast()
method throws an exception. Also, remove(element)
throws an exception if the element is not found.
For example,
import java.util.ArrayDeque; class Main ( public static void main(String() args) ( ArrayDeque animals= new ArrayDeque(); animals.add("Dog"); animals.add("Cat"); animals.add("Cow"); animals.add("Horse"); System.out.println("ArrayDeque: " + animals); // Using remove() String element = animals.remove(); System.out.println("Removed Element: " + element); System.out.println("New ArrayDeque: " + animals); // Using removeFirst() String firstElement = animals.removeFirst(); System.out.println("Removed First Element: " + firstElement); // Using removeLast() String lastElement = animals.removeLast(); System.out.println("Removed Last Element: " + lastElement); ) )
Output
ArrayDeque: (Dog, Cat, Cow, Horse) Removed Element: Dog New ArrayDeque: (Cat, Cow, Horse) Removed First Element: Cat Removed Last Element: Horse
2. Remove elements using the poll(), pollFirst() and pollLast() method
poll()
- returns and removes the first element of the array dequepollFirst()
- returns and removes the first element of the array deque (equivalent topoll()
)pollLast()
- returns and removes the last element of the array deque
Note: If the array deque is empty, poll()
, pollFirst()
and pollLast()
returns null
if the element is not found.
For example,
import java.util.ArrayDeque; class Main ( public static void main(String() args) ( ArrayDeque animals= new ArrayDeque(); animals.add("Dog"); animals.add("Cat"); animals.add("Cow"); animals.add("Horse"); System.out.println("ArrayDeque: " + animals); // Using poll() String element = animals.poll(); System.out.println("Removed Element: " + element); System.out.println("New ArrayDeque: " + animals); // Using pollFirst() String firstElement = animals.pollFirst(); System.out.println("Removed First Element: " + firstElement); // Using pollLast() String lastElement = animals.pollLast(); System.out.println("Removed Last Element: " + lastElement); ) )
Output
ArrayDeque: (Dog, Cat, Cow, Horse) Removed Element: Dog New ArrayDeque: (Cat, Cow, Horse) Removed First Element: Cat Removed Last Element: Horse
3. Remove Element: using the clear() method
To remove all the elements from the array deque, we use the clear()
method. For example,
import java.util.ArrayDeque; class Main ( public static void main(String() args) ( ArrayDeque animals= new ArrayDeque(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayDeque: " + animals); // Using clear() animals.clear(); System.out.println("New ArrayDeque: " + animals); ) )
Output
ArrayDeque: (Dog, Cat, Horse) New ArrayDeque: ()
Iterating the ArrayDeque
iterator()
- returns an iterator that can be used to iterate over the array dequedescendingIterator()
- returns an iterator that can be used to iterate over the array deque in reverse order
In order to use these methods, we must import the java.util.Iterator
package. For example,
import java.util.ArrayDeque; import java.util.Iterator; class Main ( public static void main(String() args) ( ArrayDeque animals= new ArrayDeque(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.print("ArrayDeque: "); // Using iterator() Iterator iterate = animals.iterator(); while(iterate.hasNext()) ( System.out.print(iterate.next()); System.out.print(", "); ) System.out.print("ArrayDeque in reverse order: "); // Using descendingIterator() Iterator desIterate = animals.descendingIterator(); while(desIterate.hasNext()) ( System.out.print(desIterate.next()); System.out.print(", "); ) ) )
Output
ArrayDeque: (Dog, Cat, Horse) ArrayDeque in reverse order: (Horse, Cat, Dog)
Other Methods
Methods | Descriptions |
---|---|
element() | Returns an element from the head of the array deque. |
contains(element) | Searches the array deque for the specified element. If the element is found, it returns true , if not it returns false . |
size() | Returns the length of the array deque. |
toArray() | Converts array deque to array and returns it. |
clone() | Creates a copy of the array deque and returns it. |
ArrayDeque as a Stack
To implement a LIFO (Last-In-First-Out) stacks in Java, it is recommended to use a deque over the Stack class. The ArrayDeque
class is likely to be faster than the Stack
class.
ArrayDeque
provides the following methods that can be used for implementing a stack.
push()
- adds an element to the top of the stackpeek()
- returns an element from the top of the stackpop()
- returns and removes an element from the top of the stack
For example,
import java.util.ArrayDeque; class Main ( public static void main(String() args) ( ArrayDeque stack = new ArrayDeque(); // Add elements to stack stack.push("Dog"); stack.push("Cat"); stack.push("Horse"); System.out.println("Stack: " + stack); // Access element from top of stack String element = stack.peek(); System.out.println("Accessed Element: " + element); // Remove elements from top of stack String remElement = stack.pop(); System.out.println("Removed element: " + remElement); ) )
Output
Stack: (Horse, Cat, Dog) Accessed Element: Horse Removed Element: Horse
ArrayDeque Vs. LinkedList Class
Both ArrayDeque
and Java LinkedList implements the Deque
interface. However, there exist some differences between them.
LinkedList
supportsnull
elements, whereasArrayDeque
doesn't.- लिंक की गई सूची में प्रत्येक नोड में अन्य नोड्स के लिंक शामिल हैं। इसलिए
LinkedList
आवश्यकता से अधिक भंडारण की आवश्यकता हैArrayDeque
। - यदि आप कतार या deque डेटा संरचना को लागू कर रहे हैं,
ArrayDeque
तो a की तुलना में तेज़ होने की संभावना हैLinkedList
।