当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java AbstractSequentialList set()用法及代码示例


Java.util.AbstractSequentialList的set()方法用于将使用LinkedList类创建的顺序列表中的任何特定元素替换为另一个元素。这可以通过在set()方法的参数中指定要替换的元素的位置和新元素来完成。

用法:

AbstractSequentialList.set(int index, Object element)

参数:该函数接受两个参数,分别说明如下。


  • index:这是整数类型,是指要从列表中替换的元素的位置。
  • element:这是将替换现有元素的新元素,并且与顺序列表具有相同的对象类型。

返回值:该方法从顺序列表中返回先前的值,该值被新值替换。

以下示例程序旨在说明Java.util.AbstractSequentialList.set()方法:

范例1:

// Java code to illustrate set() 
  
import java.util.*; 
import java.util.AbstractSequentialList; 
  
public class AbstractSequentialListDemo { 
    public static void main(String args[]) 
    { 
        // Creating an empty AbstractSequentialList 
        AbstractSequentialList<String> 
            absqlist = new LinkedList<String>(); 
  
        // Use add() method to add elements in the list 
        absqlist.add("Geeks"); 
        absqlist.add("for"); 
        absqlist.add("Geeks"); 
        absqlist.add("10"); 
        absqlist.add("20"); 
  
        // Displaying the AbstractSequentialList 
        System.out.println("AbstractSequentialList:"
                           + absqlist); 
  
        // Using set() method to replace Geeks with GFG 
        System.out.println("The Object that is replaced is:"
                           + absqlist.set(2, "GFG")); 
  
        // Using set() method to replace 20 with 50 
        System.out.println("The Object that is replaced is:"
                           + absqlist.set(4, "50")); 
  
        // Displaying the modified linkedlist 
        System.out.println("The new List is:" + absqlist); 
    } 
}
输出:
AbstractSequentialList:[Geeks, for, Geeks, 10, 20]
The Object that is replaced is:Geeks
The Object that is replaced is:20
The new List is:[Geeks, for, GFG, 10, 50]

范例2:

// Java code to illustrate set() 
  
import java.util.*; 
import java.util.AbstractSequentialList; 
  
public class AbstractSequentialListDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty AbstractSequentialList 
        AbstractSequentialList<Integer> 
            absqlist = new LinkedList<Integer>(); 
  
        // Use add() method to add elements in the list 
        absqlist.add(50); 
        absqlist.add(40); 
        absqlist.add(30); 
        absqlist.add(20); 
        absqlist.add(10); 
  
        // Displaying the AbstractSequentialList 
        System.out.println("AbstractSequentialList:"
                           + absqlist); 
  
        // Using set() method to replace 40 with 400 
        System.out.println("The Object that is replaced is:"
                           + absqlist.set(1, 400)); 
  
        // Using set() method to replace 10 with 100 
        System.out.println("The Object that is replaced is:"
                           + absqlist.set(4, 100)); 
  
        // Displaying the modified linkedlist 
        System.out.println("The new List is:" + absqlist); 
    } 
}
输出:
AbstractSequentialList:[50, 40, 30, 20, 10]
The Object that is replaced is:40
The Object that is replaced is:10
The new List is:[50, 400, 30, 20, 100]


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 AbstractSequentialList set() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。