Java AbstractSequentialList 类中的 set() 方法
AbtstractSequentialList 类中的 set() 方法用于将此列表中指定位置处的元素替换为指定的元素。它返回被替换的元素。语法如下所示 −
E set(int index, E element)
在此,index 是要替换元素的索引。ele 是要存储在指定位置的元素。
要在 Java 中使用 AbstractSequentialList 类,需要导入以下软件包 −
import java.util.AbstractSequentialList;
下面是一个在 Java 中实现 AbstractSequentialList set() 方法的示例 −
示例
import java.util.LinkedList; import java.util.AbstractSequentialList; public class Demo { public static void main(String[] args) { AbstractSequentialList<Integer> absSequential = new LinkedList<>(); absSequential.add(250); absSequential.add(320); absSequential.add(400); absSequential.add(550); absSequential.add(600); absSequential.add(700); absSequential.add(900); System.out.println("Elements in the AbstractSequentialList = "+absSequential); System.out.println("Replaced = " + absSequential.set(4,650 )); System.out.println("Updated AbstractSequentialList = "+absSequential); } }
输出
Elements in the AbstractSequentialList = [250, 320, 400, 550, 600, 700, 900] Replaced = 600 Updated AbstractSequentialList = [250, 320, 400, 550, 650, 700, 900]
广告