Java.util.LinkedList.clone() Method


Advertisements


Description

The java.util.LinkedList.clone() method returns a shallow copy of this LinkedList.

Declaration

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

public Object clone()

Parameters

  • NA

Return Value

This method returns a shallow copy of this LinkedList instance

Exception

  • NA

Example

The following example shows the usage of java.util.LinkedList.clone() method.

package com.tutorialspoint;

import java.util.*;

public class LinkedListDemo {

   public static void main(String[] args) {

   // create a LinkedList
   LinkedList list1 = new LinkedList();

   // add some elements
   list1.add("Hello");
   list1.add(2);
   list1.add("Chocolate");
   list1.add("10");

   // print the list
   System.out.println("LinkedList:" + list1);

   // create a second LinkedList
   LinkedList list2 = new LinkedList();

   // clone list1
   list2 = (LinkedList) list1.clone();

   // print list2
   System.out.println("LinkedList 2:" + list2);
   }
}

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

LinkedList:[Hello, 2, Chocolate, 10]
LinkedList 2:[Hello, 2, Chocolate, 10]

java_util_linkedlist.htm

Advertisements