Skip to main content

8. Object clone()

8. Object clone()
Returns a shallow copy of this LinkedList.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.spy.method.LinkedList;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class Clone 
{
 
	public static void main(String[] args)
	{
 
		LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
		//Retrieving LinkedList element
 
		for (String s : emp_list) 
		{
 
			System.out.println(s);
		}
 
		//Making shallow copy
 
		LinkedList<String> copy_emp=(LinkedList)emp_list.clone();	
 
		System.out.println("Retrieving element from shallo copy of Arraylist");
 
		//Retrieving Arraylist element of shallow copy
		for (String s : copy_emp) 
		{
 
			System.out.println(s);
		}
 
	}
 
}

output

1
2
3
4
5
6
7
8
Abhilash Shah
Nilay Shah
Vaibhav Prajapati
Retrieving element from shallo copy of Arraylist
Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Vaibhav Prajapati