Java LinkedList对象的set(int index,Object o)方法和示例

LinkedList对象set(int index,Object o)方法 (LinkedList Object set(int index , Object o) method)

  • This method is available in package java.util.LinkedList.set(int index , Object o).

    软件包java.util.LinkedList.set(int index,Object o)中提供了此方法。

  • This method is used to replace the indexed element with the specified element in the linked list.

    此方法用于用链接列表中的指定元素替换索引元素。

  • This method is used to sets a new element at the specified position.

    此方法用于在指定位置设置新元素。

Syntax:

句法:

    Object set(int index , Object o){
    }

Parameter(s):

参数:

We can pass two objects as a parameter in the method the first object is index (i.e. This is of integer or number type and refers to the position of the element that is to be replaced from the linked list.) and second parameter is Object (i.e. It is the inserted element by which the existing element will be replaced of the linked list.).

我们可以在方法中传递两个对象作为参数,第一个对象是索引(即,它是整数或数字类型,是指要从链表中替换的元素的位置。),第二个参数是Object(即它是插入的元素,通过该元素将替换现有的链表。)

Return value:

返回值:

The return type of this method is Object that means this method returns the old or previous element of the linked list that is replaced with the new element.

此方法的返回类型为Object,这表示此方法返回链表的旧元素或上一个元素,并用新元素替换。

Java程序演示LinkedList set()方法的示例 (Java program to demonstrate example of LinkedList set() method)

import java.util.LinkedList;

public class LinkList {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        // use add() method to add elements in the list 
        list.add("J");
        list.add("A");
        list.add("V");
        list.add("A");
        list.add("LANGUAGE");

        //  Current list Output
        System.out.println("The Current list is:" + list);


        // Replace old element with the new element at the specified position 
        // by implementing set(int index , Object o) of the linked list .
        System.out.println("The previous element (replaced with the new element) of the linked list is:" + list.set(4, "PROGRAMMING"));

        // Display linked list after set(int index , Object o) .
        System.out.println("The new linked list after set(int index, Object o) is:" + list);
    }
}

Output

输出量

D:\Programs>javac LinkList.java

D:\Programs>java LinkList
The Current list is:[J, A, V, A, LANGUAGE]
The previous element (replaced with the new element) of the linked list is:LANGUAGE
The new linked list after set(int index, Object o) is:[J, A, V, A, PROGRAMMING]


翻译自: https://www.includehelp.com/java/linkedlist-object-set-int-index-object-o-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值