Home » Java programming language

Java LinkedList Object clone() method with Example

Java LinkedList Object clone() method: Here, we are going to learn about the Object clone() method of LinkedList class with its syntax and example.
Submitted by Preeti Jain, on June 17, 2019

LinkedList Object clone() method

  • This method is available in package java.util.Collection and here, Collection is an interface.
  • This method is used to create a duplicate or shallow copy of the Linked list.
  • In this method, we required two objects of the same type and one object will be copied in another object.
  • This method does not return an exception.

Syntax:

    Object  clone(){
    }

Parameter(s):

This method does not accept any parameter.

Return value:

The return type of this method is Object that means this method returns an instance of the linked list after execution.

Java program to demonstrate example of LinkedList clone() method

import java.util.LinkedList;

public class LinkList {
 public static void main(String[] args) {

  // Create an object of linked list 1
  LinkedList l1 = new LinkedList();

  // Create an object of linked list 2
  LinkedList l2 = new LinkedList();

  // use add() method to add few elements in the linked list 1 
  l1.add(10);
  l1.add(20);
  l1.add(30);
  l1.add(40);
  l1.add(50);

  //  Linked list 1 Output
  System.out.println("The  Linked list 1 is :" + l1);

  // With the help of clone() we will copy of 
  // all the elements of linked list 1 in other 
  // linked list 2 without inserting manually
  l2 = (LinkedList) l1.clone();

  //  Linked list 2 Output
  System.out.println("The Linked List 2 is:" + l2);
 }
}

Output

D:\Programs>javac LinkList.java

D:\Programs>java LinkList
The  Linked list 1 is :[10, 20, 30, 40, 50]
The Linked List 2 is:[10, 20, 30, 40, 50]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.