Java LinkedList对象的clone()方法和示例

LinkedList对象clone()方法 (LinkedList Object clone() method)

  • This method is available in package java.util.Collection and here, Collection is an interface.

    该方法在java.util.Collection包中可用,在这里, Collection是一个接口。

  • 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.

该方法的返回类型为Object ,这意味着该方法在执行后返回链表的实例。

Java程序,演示LinkedList clone()方法的示例 (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]


翻译自: https://www.includehelp.com/java/linkedlist-object-clone-method-with-example.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值