GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java ArrayDeque clone() Method
Selected Reading
Java ArrayDeque clone() MethodDescriptionThe Java ArrayDeque clone() method returns a copy of this deque. This cloning helps in preventing the side-effects of manipulating the copy of object. DeclarationFollowing is the declaration for java.util.ArrayDeque.clone() method public ArrayDeque<E> clone() ParametersNA Return ValueThis method returns a copy of this deque. ExceptionNA Cloning an ArrayDeque of Integers ExampleThe 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);
}
}
OutputLet us compile and run the above program, this will produce the following result − ArrayDeque = [1, 2, 3, 4] Cloning an ArrayDeque of Strings ExampleThe 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);
}
}
OutputLet us compile and run the above program, this will produce the following result − ArrayDeque = [A, B, C, D] Cloning an ArrayDeque of Objects ExampleThe 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 + " ]";
}
}
OutputLet 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
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |