Java.util.ArrayDeque.clone() Method

Advertisements


Description

The java.util.ArrayDeque.clone() method returns a copy of this deque.

Declaration

Following is the declaration for java.util.ArrayDeque.clone() method

public ArrayDeque<E> clone()

Parameters

  • NA

Return Value

This method returns a copy of this deque.

Exception

  • NA

Example

The following example shows the usage of java.util.ArrayDeque.getFirst() method.

package com.tutorialspoint;

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

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque with an initial capacity
      ArrayDeque<Integer> deque1 = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque1.add(15);
      deque1.add(30);
      deque1.add(20);
      deque1.add(18);
      
      // clone the first deque,
      Deque<Integer> deque2 = deque1.clone();
           
      // let us print all the elements available in deque2
      // now deque2 should have similar elements to deque1.
      for (Integer number : deque2) {
         System.out.println("Number = " + number);
      }
   }
}

Let us compile and run the above program, this will produce the following result:

Number = 15
Number = 30
Number = 20
Number = 18



Advertisements
Advertisements