Java ArrayDeque clone() Method



Description

The Java ArrayDeque clone() method returns a copy of this deque. This cloning helps in preventing the side-effects of manipulating the copy of object.

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 1

The following example shows the usage of Java ArrayDeque clone() method. In this example, we're using Integers. At first, we're creating a deque1 as a new ArrayDeque object and then initialize with few items. As next step, we're cloning the deque1 to deque2 using clone() method call on deque1 object. In the end, we're printing deque2 to check if it contains copy of all elements of deque1 object.

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
      ArrayDeque<Integer> deque1 = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque1.add(1);
      deque1.add(2);
      deque1.add(3);
      deque1.add(4);
         
      // 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.
      System.out.println("ArrayDeque = " + deque2);
   }
}

Output

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

ArrayDeque = [1, 2, 3, 4]

Example 2

The following example shows the usage of Java ArrayDeque clone() method. In this example, we're using Strings. At first, we're creating a deque1 as a new ArrayDeque object and then initialize with few items. As next step, we're cloning the deque1 to deque2 using clone() method call on deque1 object. In the end, we're printing deque2 to check if it contains copy of all elements of deque1 object.

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
      ArrayDeque<String> deque1 = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque1.add("A");
      deque1.add("B");
      deque1.add("C");
      deque1.add("D");
         
      // clone the first deque,
      Deque<String> deque2 = deque1.clone();

      // let us print all the elements available in deque2
      // now deque2 should have similar elements to deque1.
      System.out.println("ArrayDeque = " + deque2);
   }
}

Output

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

ArrayDeque = [A, B, C, D]

Example 3

The following example shows the usage of Java ArrayDeque clone() method. In this example, we're using Student objects. At first, we're creating a deque1 as a new ArrayDeque object and then initialize with few items. As next step, we're cloning the deque1 to deque2 using clone() method call on deque1 object. In the end, we're printing deque2 to check if it contains copy of all elements of deque1 object.

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
      ArrayDeque<Student> deque1 = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque1.add(new Student(1, "Julie"));
      deque1.add(new Student(2, "Robert"));
      deque1.add(new Student(3, "Adam"));
         
      // clone the first deque,
      Deque<Student> deque2 = deque1.clone();

      // let us print all the elements available in deque2
      // now deque2 should have similar elements to deque1.
      System.out.println("ArrayDeque = " + deque2);
   }
}
class Student {
   int rollNo;
   String name;
   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

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

ArrayDeque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_arraydeque.htm
Advertisements