Σε αυτό το σεμινάριο, θα μάθουμε για την τάξη ArrayDeque και τις μεθόδους της με τη βοήθεια παραδειγμάτων. Επίσης, θα μάθουμε να χρησιμοποιούμε array deque για να εφαρμόσουμε μια στοίβα.
Στην Java, μπορούμε να χρησιμοποιήσουμε την ArrayDeque
κλάση για να εφαρμόσουμε δομές δεδομένων ουράς και deque χρησιμοποιώντας πίνακες.
Διεπαφές που εφαρμόστηκαν από το ArrayDeque
Η ArrayDeque
τάξη εφαρμόζει αυτές τις δύο διεπαφές:
- Διεπαφή ουράς Java
- Διεπαφή Java Deque
Δημιουργία ArrayDeque
Για να δημιουργήσουμε ένα deque πίνακα, πρέπει να εισαγάγουμε το java.util.ArrayDeque
πακέτο.
Εδώ είναι πώς μπορούμε να δημιουργήσουμε έναν πίνακα deque στην Java:
ArrayDeque animal = new ArrayDeque();
Εδώ, το Type υποδεικνύει τον τύπο του πίνακα 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()
- εισάγει το καθορισμένο στοιχείο στο τέλος του πίνακα dequeaddFirst()
- εισάγει το καθορισμένο στοιχείο στην αρχή του πίνακα dequeaddLast()
- εισάγει το καθορισμένο στο τέλος του πίνακα deque (ισοδύναμο μεadd()
)
Σημείωση: Εάν η deque σειρά είναι πλήρης, όλες αυτές οι μέθοδοι 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. Εισαγάγετε στοιχεία χρησιμοποιώντας προσφορά (), offerFirst () και offerLast ()
offer()
- εισάγει το καθορισμένο στοιχείο στο τέλος του πίνακα dequeofferFirst()
- εισάγει το καθορισμένο στοιχείο στην αρχή του πίνακα dequeofferLast()
- εισάγει το καθορισμένο στοιχείο στο τέλος του πίνακα deque
Σημείωση: 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
Πρόσβαση στα στοιχεία ArrayDeque
1. Πρόσβαση σε στοιχεία χρησιμοποιώντας το getFirst () και το getLast ()
getFirst()
- επιστρέφει το πρώτο στοιχείο του πίνακα dequegetLast()
- επιστρέφει το τελευταίο στοιχείο του πίνακα deque
Σημείωση: Εάν το deque του πίνακα είναι κενό 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 () και peekLast ()
peek()
- επιστρέφει το πρώτο στοιχείο του πίνακα dequepeekFirst()
- επιστρέφει το πρώτο στοιχείο του πίνακα deque (ισοδύναμο μεpeek()
)peekLast()
- επιστρέφει το τελευταίο στοιχείο του πίνακα deque
Για παράδειγμα,
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); ) )
Παραγωγή
ArrayDeque: (Dog, Cat, Horse) Head Element: Dog First Element: Dog Last Element: Horse
Σημείωση: Εάν ο πίνακας deque είναι κενός peek()
, peekFirst()
και getLast()
ρίχνει NoSuchElementException
.
Αφαιρέστε τα στοιχεία ArrayDeque
1. Αφαιρέστε τα στοιχεία χρησιμοποιώντας τη μέθοδο remove (), removeFirst (), removeLast ()
remove()
- επιστρέφει και αφαιρεί ένα στοιχείο από το πρώτο στοιχείο του πίνακα dequeremove(element)
- επιστρέφει και αφαιρεί το καθορισμένο στοιχείο από την κεφαλή του πίνακα dequeremoveFirst()
- επιστρέφει και αφαιρεί το πρώτο στοιχείο από τον πίνακα deque (ισοδύναμο μεremove()
)removeLast()
- επιστρέφει και αφαιρεί το τελευταίο στοιχείο από το 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
είναι πιθανό να γρηγορότερα από ό, τι έναLinkedList
.