大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
ArrayDeque in Java
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:
- Array deques have no capacity restrictions and they grow as necessary to support usage.
- They are not thread-safe which means that in the absence of external synchronization, ArrayDeque does not support concurrent access by multiple threads.
- Null elements are prohibited in the ArrayDeque.
- ArrayDeque class is likely to be faster than Stack when used as a stack.
- ArrayDeque class is likely to be faster than LinkedList when used as a queue.
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:
- ArrayDeque(): Used to create an empty ArrayDeque and by default holds an initial capacity to hold 16 elements.
- ArrayDeque(Collection c): Used to create an ArrayDeque containing all the elements same as that of the specified collection.
- ArrayDeque(int numofElements): Used to create an empty ArrayDeque wand holds the capacity to contain a specified number of elements.
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:
- add(Element e) : The method inserts particular element at the end of the deque.
- addFirst(Element e) : The method inserts particular element at the start of the deque.
- addLast(Element e) : The method inserts particular element at the end of the deque. It is similiar to add() method
- clear() : The method removes all deque elements.
- size() : The method returns the no. of elements in deque.
- clone() : The method copies the deque.
- contains(Obj) : The method checks whether a deque contains the element or not
- Iterator() : The method returns an iterator over the deque.
- descendingIterator() : The method returns a reverse order iterator over the deque
- element() : The method returns element at the head of the deque
- getFirst(): The method returns first element of the deque
- getLast(): The method returns last element of the deque
- isEmpty(): The method checks whether the deque is empty or not.
- toArray(): The method returns array having the elements of deque.
- offer(Element e) : The method inserts element at the end of deque.
- offerFirst(Element e) : The method inserts element at the front of deque.
- offerLast(Element e) : The method inserts element at the end of deque.
- peek() : The method returns head element without removing it.
- peekFirst() : The method returns first element without removing it.
- peekLast() : The method returns last element without removing it.
- poll() : The method returns head element and also removes it
- pollFirst() : The method returns first element and also removes it
- pollLast() : The method returns last element and also removes it
- pop() : The method pops out an element for stack repesented by deque
- push(Element e) : The method pushes an element onto stack repesented by deque
- remove() : The method returns head element and also removes it
- removeFirst() : The method returns first element and also removes it
- removeLast() : The method returns last element and also removes it
- removeFirstOccurrence(Obj) : The method removes the element where it first occur in the deque.
- removeLastOccurrence(Obj) : The method removes the element where it last occur in the deque.
ArrayDeque addFirst() Method in Java
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]
ArrayDeque add() Method in Java
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]
ArrayDeque addLast() Method in Java
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]
ArrayDeque getFirst() Method in Java
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
ArrayDeque isEmpty() Method in Java
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
ArrayDeque clear() Method in Java
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: []
ArrayDeque size() Method in Java
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
ArrayDeque contains() Method in Java
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
ArrayDeque iterator() Method in Java
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
ArrayDeque getLast() Method in Java
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
ArrayDeque element() Method in Java
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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/191300.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...