大家好,又见面了,我是你们的朋友全栈君。
ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both the sides of the queue. Few important features of ArrayDeque are as follows:
Declaration:
public class ArrayDeque Element
extends AbstractCollection
implements DequeElement, Cloneable, Serializable
Here, Element refers to the element which can refer to any class, such as Integer or Stringclass.
Constructors in ArrayDeque:
Example:
// Java program to demonstrate few functions of
// ArrayDeque in Java
import java.util.*;
public class ArrayDequeDemo
{
public static void main(String[] args)
{
// Intializing an deque
Deque<Integer> de_que = new ArrayDeque<Integer>(10);
// add() method to insert
de_que.add(10);
de_que.add(20);
de_que.add(30);
de_que.add(40);
de_que.add(50);
for (Integer element : de_que)
{
System.out.println("Element : " + element);
}
System.out.println("Using clear() ");
// clear() method
de_que.clear();
// addFirst() method to insert at start
de_que.addFirst(564);
de_que.addFirst(291);
// addLast() method to insert at end
de_que.addLast(24);
de_que.addLast(14);
System.out.println("Above elements are removed now");
// Iterator() :
System.out.println("Elements of deque using Iterator :");
for(Iterator itr = de_que.iterator(); itr.hasNext();)
{
System.out.println(itr.next());
}
// descendingIterator() : to reverse the deque order
System.out.println("Elements of deque in reverse order :");
for(Iterator dItr = de_que.descendingIterator();
dItr.hasNext();)
{
System.out.println(dItr.next());
}
// element() method : to get Head element
System.out.println("\nHead Element using element(): " +
de_que.element());
// getFirst() method : to get Head element
System.out.println("Head Element using getFirst(): " +
de_que.getFirst());
// getLast() method : to get last element
System.out.println("Last Element using getLast(): " +
de_que.getLast());
// toArray() method :
Object[] arr = de_que.toArray();
System.out.println("\nArray Size : " + arr.length);
System.out.print("Array elements : ");
for(int i=0; i<arr.length ; i++)
System.out.print(" " + arr[i]);
// peek() method : to get head
System.out.println("\nHead element : " + de_que.peek());
// poll() method : to get head
System.out.println("Head element poll : " + de_que.poll());
// push() method :
de_que.push(265);
de_que.push(984);
de_que.push(2365);
// remove() method : to get head
System.out.println("Head element remove : " + de_que.remove());
System.out.println("The final array is: "+de_que);
}
}
Output:
Element : 10
Element : 20
Element : 30
Element : 40
Element : 50
Using clear()
Above elements are removed now
Elements of deque using Iterator :
291
564
24
14
Elements of deque in reverse order :
14
24
564
291
Head Element using element(): 291
Head Element using getFirst(): 291
Last Element using getLast(): 14
Array Size : 4
Array elements : 291 564 24 14
Head element : 291
Head element poll : 291
Head element remove : 2365
The final array is: [984, 265, 564, 24, 14]
Methods in ArrayDeque:
The java.util.ArrayDeque.addFirst(Object element) method in Java is used to insert a specific element at the front of this deque.
Syntax:
Array_Deque.addFirst(Object element)
Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added.
Return Value: The function does not return any value.
Exceptions: The method throws NullPointerException if the passed parameter is NULL.
Below programs illustrate the Java.util.ArrayDeque.addFirst() method: Program 1:
// Java code to illustrate addFirst()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<Integer> de_que = new ArrayDeque<Integer>();
// Use add() method to add elements into the Deque
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Adding elements at front
de_que.addFirst(40);
de_que.addFirst(50);
de_que.addFirst(60);
de_que.addFirst(70);
// Displaying the ArrayDeque
System.out.println("ArrayDeque_front_addition: " + de_que);
// Adding elements using add()
de_que.add(1);
de_que.add(2);
de_que.add(3);
// Displaying the final ArrayDeque
System.out.println("Final ArrayDeque: " + de_que);
}
}
Output:
ArrayDeque: [10, 15, 30, 20, 5]
ArrayDeque_front_addition: [70, 60, 50, 40, 10, 15, 30, 20, 5]
Final ArrayDeque: [70, 60, 50, 40, 10, 15, 30, 20, 5, 1, 2, 3]
The Java.util.ArrayDeque.add(Object element) method in Java is used to add a specific element at the end of the Deque. The function is similar to the addLast() method of ArrayDeque in Java.
Syntax:
Array_Deque.add(Object element)
Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added to the Deque.
Return Value: The function returns True if the element is successfully added into the deque else it returns false.
Exceptions: The method throws NullPointerException if the passed parameter is NULL.
Below programs illustrate the Java.util.ArrayDeque.add() method: Program 1: Adding String elements into the Deque.
// Java code to illustrate add()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
Program 2: Adding Integer elements into the Deque.
// Java code to illustrate add()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<Integer> de_que = new ArrayDeque<Integer>();
// Use add() method to add elements into the Deque
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
}
}
Output:
ArrayDeque: [10, 15, 30, 20, 5]
The java.util.ArrayDeque.addLast(Object element) method in Java is used to insert a specific element at the end of this deque. It is similar to the add() method in Java.
Syntax:
Array_Deque.addLast(Object element)
Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added.
Return Value: The function does not return any value.
Exceptions: The method throws NullPointerException if the passed parameter is NULL.
Below programs illustrate the Java.util.ArrayDeque.addLast() method: Program 1: Adding Integers to the Deque.
// Java code to illustrate addLast()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<Integer> de_que = new ArrayDeque<Integer>();
// Use add() method to add elements into the Deque
de_que.add(10);
de_que.add(15);
de_que.add(30);
de_que.add(20);
de_que.add(5);
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Adding elements at the end
de_que.addLast(40);
de_que.addLast(50);
de_que.addLast(60);
de_que.addLast(70);
// Displaying the ArrayDeque
System.out.println("ArrayDeque_end_addition: " + de_que);
}
}
Output:
ArrayDeque: [10, 15, 30, 20, 5]
ArrayDeque_end_addition: [10, 15, 30, 20, 5, 40, 50, 60, 70]
The java.util.ArrayDeque.getFirst() method in Java is used to retrieve or fetch the first element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the first element of the deque.
Syntax:
Array_Deque.getFirst()
Parameters: The method does not take any parameter.
Return Value: The method returns the first element present in the Deque.
Below programs illustrate the Java.util.ArrayDeque.getFirst() method:
Program 1:
// Java code to illustrate getFirst()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
ArrayDeque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Displaying the first element
System.out.println("The first element is: " +
de_que.getFirst());
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The first element is: Welcome
The Java.util.ArrayDeque.isEmpty() method in Java is used to check and verify if an ArrayDeque is empty or not. It returns True if the Deque is empty else it returns False.
Syntax:
Array_Deque.isEmpty()
Parameters: The method does not take any parameter.
Return Value: The function returns True if the deque is empty else it returns False.
Below programs illustrate the Java.util.ArrayDeque.isEmpty() method:
Program 1:
// Java code to illustrate isEmpty()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Verifying if the Deque is empty or not
System.out.println("The Deque is empty? " +
de_que.isEmpty());
// Clearing the deque
de_que.clear();
// Verifying if the Deque is empty or not
System.out.println("The Deque is empty? " +
de_que.isEmpty());
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The Deque is empty? false
The Deque is empty? true
The Java.util.ArrayDeque.clear() method in Java is used to remove all of the elements from the Deque. Using the clear() method only clears all the element from the deque and does not delete the deque. In other words, it can be said that the clear() method is used to only empty an existing ArrayDeque.
Syntax:
Array_Deque.clear()
Parameters: The method does not take any parameter.
Return Value: The function does not return any value.
Below programs illustrate the Java.util.ArrayDeque.clear() method: Program 1:
// Java code to illustrate clear()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Clearing the Deque
de_que.clear();
// Displaying the Deque
System.out.println("ArrayDeque: " + de_que);
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
ArrayDeque: []
The Java.util.ArrayDeque.size() method in Java is used to get the size of the Deque or the number of elements present in the Deque.
Syntax:
Array_Deque.size()
Parameters: The method does not take any parameter.
Return Value: The method returns the size or the number of elements present in the Deque.
Below programs illustrate the Java.util.ArrayDeque.size() method: Program 1: Adding String elements into the Deque.
// Java code to illustrate size()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Displaying the size of Deque
System.out.println("The size is: " + de_que.size());
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The size is: 5
The Java.util.ArrayDeque.contains() method in Java is used to check or verify whether a specific element is present in the Deque or not.
Syntax:
Array_Deque.contains(Object element)
Parameters: The parameter element is of the type of ArrayDeque. This is the element that needs to be tested if it is present in the deque or not.
Return Value: The method returns True if the element is present in the deque otherwise it returns False.
Below programs illustrate the Java.util.ArrayDeque.contains() method: Program 1:
// Java code to illustrate contains()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Queue
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Check for "Geeks" in the deque
System.out.println("Does the deque contains 'Geeks'? "
+ de_que.contains("Geeks"));
// Check for "4" in the deque
System.out.println("Does the deque contains '4'? "
+ de_que.contains("4"));
// Check if the deque contains "No"
System.out.println("Does the deque contains 'No'? "
+ de_que.contains("No"));
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
Does the deque contains 'Geeks'? true
Does the deque contains '4'? true
Does the deque contains 'No'? false
The Java.util.ArrayDeque.iterator() method is used to return an iterator of the elements of the ArrayDeque.
Syntax:
Iterator iterate_value = Array_Deque.iterator();
Parameters: The method does not take any parameter.
Return Value: The method iterates over the elements of the deque and returns the values(iterator).
Below programs illustrate the Java.util.ArrayDeque.iterator() method: Program 1:
// Java code to illustrate iterator()
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Deque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Queue
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Creating an iterator
Iterator value = de_que.iterator();
// Displaying the values after iterating through the Deque
System.out.println("The iterator values are: ");
while (value.hasNext()) {
System.out.println(value.next());
}
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The iterator values are:
Welcome
To
Geeks
4
Geeks
The java.util.ArrayDeque.getLast() method in Java is used to retrieve or fetch the last element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the last element of the deque.
Syntax:
Array_Deque.getLast()
Parameters: The method does not take any parameter.
Return Value: The method returns the last element present in the Deque.
Below programs illustrate the Java.util.ArrayDeque.getLast() method:
Program 1:
// Java code to illustrate getLast() method of ArrayDeque
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
ArrayDeque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Displaying the Last element
System.out.println("The last element is: " + de_que.getLast());
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The last element is: Geeks
The java.util.ArrayDeque.element() method in Java is used to retrieve or fetch the head of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the element.
Syntax:
Array_Deque.element()
Parameters: The method does not take any parameter.
Return Value: The method returns the element present at the head of the Deque.
Below programs illustrate the Java.util.ArrayDeque.element() method: Program 1:
// Java code to illustrate ArrayDeque element() method
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
ArrayDeque<String> de_que = new ArrayDeque<String>();
// Use add() method to add elements into the Deque
de_que.add("Welcome");
de_que.add("To");
de_que.add("Geeks");
de_que.add("4");
de_que.add("Geeks");
// Displaying the ArrayDeque
System.out.println("ArrayDeque: " + de_que);
// Displaying the head
System.out.println("The head element is: " + de_que.element());
}
}
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The head element is: Welcome
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/191300.html原文链接:https://javaforall.cn