Home » Java programming language

Java ArrayDeque clone() Method with Example

ArrayDeque Class clone() method: Here, we are going to learn about the clone() method of ArrayDeque Class with its syntax and example.
Submitted by Preeti Jain, on January 08, 2020

ArrayDeque Class clone() method

  • clone() Method is available in java.lang package.
  • clone() Method is used to returns a copy or clone of this deque.
  • clone() Method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • clone() Method does not throw an exception at the time of cloning all of the elements from deque.

Syntax:

    public ArrayDeque<T> clone();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is ArrayDeque<T>, it returns cloned array deque.

Example:

// Java program to demonstrate the example 
// of ArrayDeque<T> clone() method of ArrayDeque 

import java.util.*;

public class CloneOfArrayDeque {
    public static void main(String[] args) {
        // Creating an ArrayDeque with initial capacity of
        // storing elements
        ArrayDeque < String > d_queue1 = new ArrayDeque < String > (10);

        // By using add() method to add elements
        // in ArrayDeque
        d_queue1.add("C");
        d_queue1.add("C++");
        d_queue1.add("Java");
        d_queue1.add("Php");
        d_queue1.add("DotNet");

        // Display Deque Elements
        System.out.println("d_queue1 : ");
        System.out.println("ArrayDeque Elements = " + d_queue1);

        // By using clone() method to clone the elements
        // of d_queue1 to d_queue2
        ArrayDeque < String > d_queue2 = d_queue1.clone();

        System.out.println();

        // Display Deque Elements
        System.out.println("d_queue2 = d_queue1.clone(): ");
        System.out.println("ArrayDeque = " + d_queue2);
    }
}

Output

d_queue1 : 
ArrayDeque Elements = [C, C++, Java, Php, DotNet]

d_queue2 = d_queue1.clone(): 
ArrayDeque = [C, C++, Java, Php, DotNet]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.