java.util.AbstractList 類的 set() 方法用於將使用 AbstractList 類創建的抽象列表中的任何特定元素替換為另一個元素。這可以通過在 set() 方法的參數中指定要替換的元素的位置和新元素來完成。
用法:
AbstractList.set(int index, Object element)
參數:該函數接受兩個參數,如下所述:
- index:這是整數類型,指的是要從抽象列表中替換的元素的位置。
- element:它是現有元素將被替換的新元素,並且與抽象列表具有相同的對象類型。
返回值:該方法從抽象列表中返回被新值替換的先前值。
以下示例程序旨在說明 AbstractList.set() 方法:
// Java code to illustrate set()
import java.util.*;
import java.util.LinkedList;
public class AbstractListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList<String> list = new LinkedList<String>();
// Use add() method to add elements in the list
list.add("Geeks");
list.add("for");
list.add("Geeks");
list.add("10");
list.add("20");
// Displaying the AbstractList
System.out.println("AbstractList:" + list);
// Using set() method to replace Geeks with GFG
System.out.println("The Object that is replaced is:"
+ list.set(2, "GFG"));
// Using set() method to replace 20 with 50
System.out.println("The Object that is replaced is:"
+ list.set(4, "50"));
// Displaying the modified AbstractList
System.out.println("The new AbstractList is:" + list);
}
}
輸出:
AbstractList:[Geeks, for, Geeks, 10, 20] The Object that is replaced is:Geeks The Object that is replaced is:20 The new AbstractList is:[Geeks, for, GFG, 10, 50]
程序2:
// Java code to illustrate set()
import java.util.*;
public class LinkedListDemo {
public static void main(String args[])
{
// Creating an empty AbstractList
AbstractList<Integer>
list = new LinkedList<Integer>();
// Use add() method to add elements in the list
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
// Displaying the AbstractList
System.out.println("AbstractList:" + list);
// Using set() method to replace 10 with 100
System.out.println("The Object that is replaced is:"
+ list.set(0, 100));
// Using set() method to replace 20 with 200
System.out.println("The Object that is replaced is:"
+ list.set(1, 200));
// Displaying the modified AbstractList
System.out.println("The new AbstractList is:" + list);
}
}
輸出:
AbstractList:[10, 20, 30, 40, 50] The Object that is replaced is:10 The Object that is replaced is:20 The new AbstractList is:[100, 200, 30, 40, 50]
相關用法
- Java AbstractList addAll()用法及代碼示例
- Java AbstractList clear()用法及代碼示例
- Java AbstractList equals()用法及代碼示例
- Java AbstractList get()用法及代碼示例
- Java AbstractList hashCode()用法及代碼示例
- Java AbstractList iterator()用法及代碼示例
- Java AbstractList lastIndexOf()用法及代碼示例
- Java AbstractList indexOf()用法及代碼示例
- Java AbstractList listIterator()用法及代碼示例
- Java AbstractList remove()用法及代碼示例
- Java AbstractList add(E ele)用法及代碼示例
- Java AbstractList subList()用法及代碼示例
- Java AbstractList add(int index, E element)用法及代碼示例
- Java AbstractList用法及代碼示例
- Java ArrayList set()用法及代碼示例
- Java AbstractSequentialList set()用法及代碼示例
- Java CopyOnWriteArrayList set()用法及代碼示例
- Java Set add()用法及代碼示例
- Java Set addAll()用法及代碼示例
- Java Set clear()用法及代碼示例
- Java Set contains()用法及代碼示例
- Java Set containsAll()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 AbstractList set() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。