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]
相關用法
- Java AbstractSequentialList get()用法及代碼示例
- Java AbstractSequentialList addAll()用法及代碼示例
- Java AbstractSequentialList remove()用法及代碼示例
- Java AbstractSequentialList用法及代碼示例
- Java AbstractSequentialList contains()用法及代碼示例
- Java AbstractSequentialList hashCode()用法及代碼示例
- Java AbstractSequentialList indexOf()用法及代碼示例
- Java AbstractSequentialList equals()用法及代碼示例
- Java AbstractSequentialList clear()用法及代碼示例
- Java AbstractSequentialList lastIndexOf()用法及代碼示例
- Java AbstractSequentialList conatinsAll()用法及代碼示例
- Java AbstractSequentialList set(int, Object)用法及代碼示例
- Java AbstractSequentialList toArray(T[])用法及代碼示例
- Java AbstractSequentialList retainAll()用法及代碼示例
- Java AbstractSequentialList toArray()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 AbstractSequentialList set() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。