w3resource

Java ArrayDeque Class: clone() Method

public ArrayDeque<E> clone()

The clone() method is used to create copy of a given deque.

Package: java.util

Java Platform: Java SE 8

Syntax:

clone()

Return Value:

a copy of this deque

Return Value Type: ArrayDeque<E - the type of elements held in this collection>

Pictorial Presentation

Java ArrayDeque Class: clone() Method

Example: Java ArrayDeque Class: clone() Method

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
   public static void main(String[] args) {
      // Create an empty array deque with an initial capacity
      ArrayDeque<Integer> deque = new ArrayDeque<Integer>(5);

      // Use add() method to add elements in the deque
      deque.add(10);
      deque.add(20);
      deque.add(30);
      deque.add(40);
      System.out.println("Original deque:");
     
     for (Integer n : deque) {
         System.out.println("Number = " + n);
      }
         
      // clone the above deque,
      Deque<Integer> cdeque = deque.clone();

     // Print the elements of the clone deque
     System.out.println("Coloned deque:");
      for (Integer n : cdeque) {
         System.out.println("Number = " + n);
      }
   }
}
 

Output:

Original deque:
Number = 10
Number = 20
Number = 30
Number = 40
Coloned deque:
Number = 10
Number = 20
Number = 30
Number = 40

Java Code Editor:

Previous: clear Method
Next: contains Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_clone.php